Search code examples
javascriptformatting

Single quotes within string


Javascript fails to read this 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!"}}';

Solution

  • Use a backslash to escape the character:

    var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';
    var parsed = JSON.parse(json);