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
  • 24,504
  • 36
  • 94
  • 124
  • 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
  • 118,520
  • 32
  • 167
  • 192
1
JSON.parse("[{\"name\":\"gath\",\"age\":10}]");

You cant have double quotes inside double quotes

austinbv
  • 9,297
  • 6
  • 50
  • 82
1

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

zebnat
  • 521
  • 3
  • 13
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
  • 267,341
  • 46
  • 444
  • 390
0

Replace

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

With

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