Bug ? JSON array returns {} from data table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Bug ? JSON array returns {} from data table
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
- Labels:
-
Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
But if I use the original fileDataBatchLines directly, it doesn't has the array functions, such as : concat, filter.
Regards,
Sean
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
ThingWorx wraps arrays into json objects with property « array ». Try fileDataBatchLines.array.filter.
/ Constantine
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I tried the statement below, but got error because fileDataBatchLines.array is undefined .
logger.debug("fileDataBatchLines.array:" + fileDataBatchLines.array);
fileDataBatchLines.array.filter(function(item){
logger.debug("item: " + item);
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hello Sean,
Sorry for the late reply.
AFAIK JavaScript array filter() method was introduced in ECMAScript 5, while ThingWorx supports JavaScript 1.7, which corresponds to ECMAScript 3 with some extensions. Under the hood ThingWorx uses Rhino 1.7r4 as its JavaScript engine. Although support for ECMAScript 5 was introduced in 1.7r3, it is not used by default, so ThingWorx sticks to JavaScript 1.7, which is described in Help and a couple of Tech Support tickets.
Regards,
Constantine
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Thank you for the explanation .
If call javaScript array concat() , it also raise function undefined error. But concat() was introduced in Javascript 1.2.
In a summary:
- fileDataBatchLines.concat //undefined
- fileDataBatchLines.array //undefined
