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

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

Unable to use JSON.stringify() on a JSON

Rishabh345
6-Contributor

Unable to use JSON.stringify() on a JSON

When trying to use JSON.stringify(), I'm getting the below error:

 

ERROR:: Can't find method com.thingworx.types.primitives.structs.Location.toJSON(string)

 

Already validated that the format of the JSON I'm trying to stringify is correct using online json validator.

Please find the JSON I'm trying to stringify attached. At the end of the file you can find the Location property, which when removed, stringify() is working fine. I'm not able to understand why it's not working if the format is correct.

Any help is greatly 
appreciated

1 ACCEPTED SOLUTION

Accepted Solutions
Rishabh345
6-Contributor
(To:slangley)

Hey @slangley , Good day!

We're currently using Thingworx 8.5.19 version.

Yes, the stringify function is not working on the attached json, but the good news is that we found a work around so there is no blocker.

JSON.stringify(), was used, because when we were writing the timestamp value to the JSON object they were inserted into the object in some invalid format, after using stringify() on the complete JSON, it was coming in proper format (example: "2022-05-15T21:56:16.133Z"). Issue arise when we added location to the JSON.

Work around: used the below snippet of code while inserting the timestamp into the object /JSON
String(new Date((TimestampValue)).toISOString()).

Now we are not using the stringify(), but the above code instead.

View solution in original post

4 REPLIES 4

@Rishabh345 

 

I created a service with input type JSON and output type string and executed following code result = JSON.stringify(jsonInput);

It executed without any error when I passed your JSON in input. 

Can you share your service code?

 

Regards,

Sachin Sharma

Please find the file attached.

 

There are 2 main functions 

  1.  getLocationHistory()
    1. is used to query the location history of an asset and that data table is passed to the next function to convert it to JSON
  2. function preparePayload ()
    1. convert the table to JSON, that will be stringified and then parsed for further use

The JSON that you parsed previously, we got it by converting the infotable to JSON using preparePayload().

 

please let me know if there is any confusion and thanks for your response!

slangley
23-Emerald II
(To:Rishabh345)

Hi @Rishabh345.

 

Which version of ThingWorx are you running?  Are you saying that you're still having an issue when you try to stringify the json you attached to your post?

 

Have you checked the ThingWorx logs?

 

Regards.

 

--Sharon

Rishabh345
6-Contributor
(To:slangley)

Hey @slangley , Good day!

We're currently using Thingworx 8.5.19 version.

Yes, the stringify function is not working on the attached json, but the good news is that we found a work around so there is no blocker.

JSON.stringify(), was used, because when we were writing the timestamp value to the JSON object they were inserted into the object in some invalid format, after using stringify() on the complete JSON, it was coming in proper format (example: "2022-05-15T21:56:16.133Z"). Issue arise when we added location to the JSON.

Work around: used the below snippet of code while inserting the timestamp into the object /JSON
String(new Date((TimestampValue)).toISOString()).

Now we are not using the stringify(), but the above code instead.
Top Tags