-4

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

 "{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}"

I'm doing it like so:

var strJSON = "{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}";
console.log(JSON.parse(strJSON));

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?

laurent
  • 88,262
  • 77
  • 290
  • 428
Emmanuel Villegas
  • 1,374
  • 3
  • 12
  • 19

4 Answers4

4

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, "\""));
console.log(o)
Rajesh
  • 24,354
  • 5
  • 48
  • 79
3

Single quotes are not valid for strings, you need to use double quotes instead:

var strJSON = '{"firstname":"Jesper","surname":"Aaberg","phone":"555-0100"}';
laurent
  • 88,262
  • 77
  • 290
  • 428
1

Just change your string:

"{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}"

to:

'{"firstname":"Jesper","surname":"Aaberg","phone":"555-0100"}'

JSON only supports double-quotes

Lucas Gabriel Sánchez
  • 40,116
  • 20
  • 56
  • 83
0

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

console.log(JSON.parse(str));

Use this.

Atul Sharma
  • 9,397
  • 10
  • 38
  • 65