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

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

JSON.stringify does not work for JSON array ?

seanccc
17-Peridot

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

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

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.

@VladimirRosu ,

Thank you . The workaround works. 

 

Regards,

Sean

Top Tags