JSON.parse not parsing the string
- March 13, 2019
- 4 replies
- 5955 views
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

