1

Am trying this on my chrome debugger console, and am getting a SyntaxError;

JSON.parse("[{"name":"gath","age":10}]");
>SyntaxError

What is the correct way of parsing the JSON string? Please note this question is a follow up to my earlier one, which am yet to get an answer!

Community
  • 1
  • 1
gath
  • 23,248
  • 36
  • 93
  • 123
  • 1
    For the record, the JSON syntax is correct, but you cannot have unescaped double quotes inside a double-quoted string in JavaScript (and probably in no other language ;)). – Felix Kling Aug 12 '11 at 14:26

5 Answers5

3

You need to escape your double-quotes.

JSON.parse("[{\"name\":\"gath\",\"age\":10}]");

or, for better readabilty, define the string with single quotes:

JSON.parse('[{"name":"gath","age":10}]');
Rob Hruska
  • 115,151
  • 29
  • 164
  • 188
1
JSON.parse("[{\"name\":\"gath\",\"age\":10}]");

You cant have double quotes inside double quotes

austinbv
  • 8,929
  • 6
  • 44
  • 80
1

You need to escape the " or do JSON.parse('[{"name":"gath","age":10}]');

zebnat
  • 511
  • 2
  • 12
0

Enclose it in single quotes and it will parse correctly.

JSON.parse('[{"name":"gath","age":10}]');


Object
age: 10
name: "gath"
__proto__: Object
Michael Berkowski
  • 260,803
  • 45
  • 432
  • 377
0

Replace

JSON.parse("[{"name":"gath","age":10}]");

With

JSON.parse('[{"name":"gath","age":10}]');
cutsoy
  • 9,979
  • 4
  • 38
  • 57