Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single quotes within json value [duplicate]

Tags:

Javascript fails to read this json string as it contains a single quote character which it sees as the end of the string.

How can I escape the single quote so that it is not seen as the end of the string?

var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It's a test!"}}';  var parsed = JSON.parse(json); 
like image 442
ezero Avatar asked Aug 21 '15 15:08

ezero


People also ask

Do I need to escape single quotes in JSON?

In JSON, you don't need to escape single quotes inside a value that is enclosed with double-quotes. In the case that you have double quotes as part of a value (that is enclosed with double quotes) then you would need to escape them.

Can JSON read single quotes?

The JSON standard requires double quotes and will not accept single quotes, nor will the parser.

Does JSON have to be double quotes?

JSON names require double quotes.


1 Answers

Use a backslash to escape the character:

var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}'; var parsed = JSON.parse(json); 
like image 149
CodingIntrigue Avatar answered Oct 01 '22 08:10

CodingIntrigue