1

I have a valid JSON:

{"name":"tono","html":"<p><a href=\"http:\/\/someurl.com\">Here<\/a> is the link<\/p>"}  

But, when I parse it through javascript (I use firefox's console)

JSON.parse('{"name":"tono","html":"<p><a href=\"http:\/\/someurl.com\">Here<\/a> is the link<\/p>"}');

I get this error

SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 36 of the JSON data

Is it an expected behavior? And how to parse JSON which contains URL correctly?

Just for additional information, this one works:

JSON.parse('{"name":"tono","html":"<p><a href=>Here<\/a> is the link<\/p>"}');

Object { name: "tono", html: "<p><a href=>Here</a> is the link</p>" }

More additional information:

The JSON parsed flawlessly here: http://jsonviewer.stack.hu

goFrendiAsgard
  • 4,016
  • 8
  • 38
  • 64

2 Answers2

3

Escape the backslashes in a string literal so they'll be treated literally.

console.log(JSON.parse('{"name":"tono","html":"<p><a href=\\"http:\\/\\/someurl.com\\">Here<\\/a> is the link<\\/p>"}'));

The reason why the forward slashes are escaped is explained here JSON: why are forward slashes escaped?

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
-2

As Barmar explained, you can escape the backslashes like such.

JSON.parse('{"name":"tono","html":"<p><a href=\\"http://someurl.com\\">Here</a> is the link</p>"}');
lksmth
  • 22
  • 5