Kyle
Kyle

Reputation: 1183

String to JSON object

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 '

Upvotes: 0

Views: 71

Answers (1)

ikegami
ikegami

Reputation: 385829

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

isn't valid JSON. You want:

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

Upvotes: 5

Related Questions