cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

JSON.stringify does not work for JSON array ?

SOLVED
Highlighted
Pearl

JSON.stringify does not work for JSON array ?

Hi ,

The JSON array behaves very strange: 

------------------test code of TestInputAsJson--------------------------------------------

logger.debug("jsonParam1 " + JSON.stringify(jsonParam1));
logger.debug("jsonParam1.World.length " + jsonParam1.World.length);
logger.debug("jsonParam1.World[0] " + jsonParam1.World[0]);

logger.debug("jsonParam2 " + JSON.stringify(jsonParam2));
logger.debug("jsonParam2.length " + jsonParam2.length);

--------------------call service TestInputAsJson-----------------------------------------------

me.InputAsJson({
jsonParam1: { Hello: "bbb", World:[1 ,2,3] } /* JSON */,
jsonParam2: [1,2,3] /* JSON */
});

---------------------log result------------------------------------------------------

jsonParam1 {"Hello":"bbb","World":{}}        ==>The JSON array is not serialized
jsonParam1.World.length 3                        ==>The JSON array is actual existing although its not visible 
jsonParam1.World[0] 1                               ==>The JSON array is accessible if it's part of JSON object
jsonParam2 {"array":{}}                               ==> The JSON array become empty if it's the root 
jsonParam2.length undefined                     ==> And it's not accessible neither. 

---------------------------------------------------------------------------------------------

Why does the JSON array behavior so strange and the root JSON array even doesn't work ? 

ThingWorx 8.5.5

 

Regards,

Sean

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: JSON.stringify does not work for JSON array ?

Sean,

 

Can you use a JSON.parse internally in the logger statements, like this?

JSON.stringify(JSON.parse(jsonParam1)

 It's something related to internal Javascript-Java conversions we do, I'm not the best authority to answer why this happens.

There's an article about this here: https://www.ptc.com/en/support/article/CS330294 , planned for fix in 9.1.0, but please confirm first if the above workaround works for you.

View solution in original post

2 REPLIES 2
Highlighted

Re: JSON.stringify does not work for JSON array ?

Sean,

 

Can you use a JSON.parse internally in the logger statements, like this?

JSON.stringify(JSON.parse(jsonParam1)

 It's something related to internal Javascript-Java conversions we do, I'm not the best authority to answer why this happens.

There's an article about this here: https://www.ptc.com/en/support/article/CS330294 , planned for fix in 9.1.0, but please confirm first if the above workaround works for you.

View solution in original post

Highlighted

Re: JSON.stringify does not work for JSON array ?

@VladimirRosu ,

Thank you . The workaround works. 

 

Regards,

Sean

Announcements

Thingworx Navigate content has a new home! Click here to access the new Thingworx Navigate forum!