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

JSON.parse not parsing the string

SOLVED
Regular Member

JSON.parse not parsing the string

Hi All,

I am creating a string using concatenation of different data needed and creating the result string as below.

{"rows":[{ "id":1, "data":["Tr1",80.35,79.27]},{ "id":2, "data":["Tr2",7.24,19.34]},{ "id":3, "data":["Tr3",16.02,77.20]},{ "id":4, "data":["Tr4",44.12,51.64]},{ "id":5, "data":["Tr5",30.72,93.91]}]}

This input to JSON.parse(); gives error as "unexpected object literal".
I understand that, when the string is passed to the JSON.parse it will be in double quotes, as below

"{"rows":[{ "id":1, "data":["Tr1",80.35,79.27]},{ "id":2, "data":["Tr2",7.24,19.34]},{ "id":3, "data":["Tr3",16.02,77.20]},{ "id":4, "data":["Tr4",44.12,51.64]},{ "id":5, "data":["Tr5",30.72,93.91]}]}"

and this will fail as invalid JSON. So I am passing the string with a backslash as a prefix to the double quotes as ( \" ). so that my result String would be.

"{\"rows\":[{ \"id\":1, \"data\":[\"Tr1\",80.35,79.27]},{ \"id\":2, \"data\":[\"Tr2\",7.24,19.34]},{ \"id\":3, \"data\":[\"Tr3\",16.02,77.20]},{ \"id\":4, \"data\":[\"Tr4\",44.12,51.64]},{ \"id\":5, \"data\":[\"Tr5\",30.72,93.91]}]}"

Even this fails JSON.parse() or service keeps showing the "Executing" status. However when this same string is parsed in browser's console, it parses successfully.


In addition to above, I cannot create an Infotable and then convert it to JSON, because the column names that I have include spaces and special characters. And Thingworx doesn't accept Field Names for Datashape with Special Characters and spaces..


Bhushan

Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions

Re: JSON.parse not parsing the string

I'm not quite sure how you are doing this, but

the following piece works just fine (output of service is string)

var myString = JSON.parse('{"rows": [{"id": 1,"data": ["Tr1", 80.35, 79.27] }, {"id": 2,"data": ["Tr2", 7.24, 19.34]}]}')
result = myString.rows[0].data[1];

 

and my output is 80.35

So you should be able to iterate the object and construct an infotable.

4 REPLIES 4

Re: JSON.parse not parsing the string

What exactly are you trying to accomplish?

How is this string being put into Thingworx?

I think if you have a service that had an input parameter of JSON and you input the original string, it should automatically be interpreted as a JSON object within Thingworx and you wouldn't need to JSON.parse it.

Highlighted

Re: JSON.parse not parsing the string

Hello Paichung,

 

What I want to accomplish? - I want to create JSON for the row Data in grid. I cannot use Infotable as the headers I need for the result table have special characters and spaces and are changing continuously.

 

I am creating the string using the data from 2 tables within a service.

And then using JSON.parse on that string.

 

I tried your suggestion too. Created a Input Parameter with baseType as JSON and assigned the string variable to it. and passed to result with baseType set as JSON.

Also This time, I added logger.warn() to create log messages.


One Thing I noticed, the service execution is completed, but the window keeps on showing Executing bar continuously. Same happened when I tried with JSON.parse() and your method too.

 

This clears that there is no issue with the JSON.parse(), actually the Executing message that appears continuously.
How can I make sure, that this will not affect when using in mashups anyway.

 

Thanks,

Bhushan

Re: JSON.parse not parsing the string

I'm not quite sure how you are doing this, but

the following piece works just fine (output of service is string)

var myString = JSON.parse('{"rows": [{"id": 1,"data": ["Tr1", 80.35, 79.27] }, {"id": 2,"data": ["Tr2", 7.24, 19.34]}]}')
result = myString.rows[0].data[1];

 

and my output is 80.35

So you should be able to iterate the object and construct an infotable.

Re: JSON.parse not parsing the string

Hello Paichung,

 

It was working for me too, but I didn't know that until I added the statements to create log entries in the service code.
For me the service used to execute successfully, but the output was not displayed, instead the Service executing message kept displaying even after the execution completed.

 

Thanks,

Bhushan