Skip to main content
17-Peridot
July 27, 2019
Question

Bug ? JSON array returns {} from data table

  • July 27, 2019
  • 2 replies
  • 2978 views

Hi, 

I have a data table and one of the fields is JSON type .   I can write a JSON object into the table successfully,  but when getting the JSON object back,  the array property of the JSON object becomes {}. 

 

The workaround is that I have to convert the array field into String and then call JSON.parse to get the JSON array back . 

Is it a known issue ?  

 

I uploaded a reproducible XML file for the problem , you can run AddJsonValue firstly , then run  GetJson to observe the log  . 

 

ThingWorx 8.4.2 + PostgreSQL 10

 

Regards,

Sean

 

2 replies

18-Opal
July 29, 2019

Hello Sean,

 

The DataTables work fine, the actual issue is with JSON.stringify, which doesn't traverse complete JSON object. E.g. this code outputs "1" as expected:

logger.debug("The original json array fileDataBatchLines:" + JSON.stringify(fileDataBatchLines[0][0]));

 

We created a trivial (four lines of code) ThingWorx extension to work around this issue, here it is: https://github.com/vilia-fr/twxdevext

 

You can use it as following:

var str = Resources["DevelopFunctions"].JsonToString({ json: fileDataBatchLines });
logger.debug("The original json array fileDataBatchLines:" + str);

In the log you can see complete JSON, with "_" standing for line breaks to improve readability.

 

DISCLAIMER: We don't support this extension officially, use it on your own risk. I bet there's a better, more standard way to do it, I just don't know about it.

 

/ Constantine

18-Opal
July 29, 2019

Sorry Sean,

 

Forget about extension -- I checked, and we've made it only to put linebreaks inside the output. Just skip JSON.stringify altogether and it'll work:

logger.debug("The original json array fileDataBatchLines:" + fileDataBatchLines);

 

/ Constantine

seanccc17-PeridotAuthor
17-Peridot
July 29, 2019

@Constantine ,

But if I use the original  fileDataBatchLines directly,  it doesn't has the array functions, such as : concat, filter.   

 

Regards,

Sean

 

18-Opal
July 29, 2019
Sean,

ThingWorx wraps arrays into json objects with property « array ». Try fileDataBatchLines.array.filter.

/ Constantine