Ad

String To JSON Object

- 1 answer

I'm trying to convert a JavaScript string whose value is already formatted in JSON syntax, to a valid JSON object using JSON.parse.

// JSON formatted string
var string = "{'1451893035': 1.2,'1452670635':0.5,'1451720235': 2.5}";
// parse to JSON object
console.log(JSON.parse(string));

I'm currently getting this error:

Uncaught SyntaxError: Unexpected token '

Ad

Answer

{'1451893035': 1.2,'1452670635':0.5,'1451720235': 2.5}

isn't valid JSON. You want:

{"1451893035": 1.2,"1452670635":0.5,"1451720235": 2.5}
Ad
source: stackoverflow.com
Ad