Emmanuel Villegas Emmanuel Villegas - 6 months ago 50
Javascript Question

how to parse string into json object javascript

I'm trying to parse this string into a JSON:


I'm doing it like so:

var strJSON = "{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}";

But I get the error message:

VM652:1 Uncaught SyntaxError: Unexpected token ' in JSON at position 1
at JSON.parse ()

Does anybody know what am I missing and how can i solve it?


You can replace single quotes to double quotes and parse it.

var str =  "{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}";

var o = JSON.parse(str.replace(/\'/g, "\""));