26

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);
vela
  • 147
  • 10
ezero
  • 1,230
  • 3
  • 12
  • 27
  • 1
    Why are you writing out an object serialized as JSON, then decoding it on the next line? Just write an object literal. There's no need to put your object in a string at all. – Darth Egregious Aug 21 '15 at 15:07
  • Funny when someone asks an easy js question how there's a flood of equivalent answers within the same minute. This is surely a duplicate. – Darth Egregious Aug 21 '15 at 15:08
  • 1
    This has nothing to do with JSON. The problem is that you have a single quote in a JavaScript string literal delimited with single quotes. The fact the string contains JSON is beside the point. (Why are you writing a static string of JSON and then parsing it anyway, why not just remove the quotes from around the edge and treat it as a JavaScript object literal?) – Quentin Aug 21 '15 at 15:09
  • @Fuser97381 It's not written out, the value is coming from a database and put there by php. – ezero Aug 21 '15 at 15:12
  • @ezero — Why are you writing PHP that does that then? – Quentin Aug 21 '15 at 15:13
  • @Quentin So that a javascript object can be built up and then stored in a database as a json string which can then later be re-assembled as the same object. – ezero Aug 21 '15 at 15:14
  • And why do you need to generate a string literal of JSON and then parse it with JavaScript instead of treating that JSON as an object literal? – Quentin Aug 21 '15 at 15:19
  • @Quentin How could I treat the JSON as an object literal? – ezero Aug 21 '15 at 15:24
  • But not putting it inside a string literal. i.e. removing the quotes. Just like Fuser97381 said in the first comment. – Quentin Aug 21 '15 at 15:25
  • Also, php has `json_encode`, which you should be using if you need to go from a variable to JSON in php. – Darth Egregious Aug 21 '15 at 15:27
  • @Quentin I was not aware that was possible, thanks! – ezero Aug 21 '15 at 15:30
  • @Fuser97381 I was already using json_encode. Using the json to create an object without first turning it into a string was the solution. – ezero Aug 21 '15 at 15:32
  • So basically the real solution to your weird question is that you should have been calling `json_encode` twice, since the second one would have escaped the values of the string representation. – Darth Egregious Aug 21 '15 at 15:45
  • 1
    @Fuser97381 No, that was not the solution. It was simply to remove the outer single quotes before php outputted the json. – ezero Aug 21 '15 at 15:49
  • json_encode would also escape the double quotes – ezero Aug 21 '15 at 20:59
  • 3
    He's providing you samples of what's happing and how to reproduce it - which surely you would have complained about if he hadn't. Think a bit more abstractly about it. There's a single apostrophe in the data. Maybe a better question would be "How should I encode this data to eliminate problems with the apostrophe?" – ChrisH Apr 27 '21 at 14:12

3 Answers3

23

Use a backslash to escape the character:

var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';
var parsed = JSON.parse(json);
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
  • Using a backslash is an example of 'escaping' a character. See more information [here](http://www.w3schools.com/js/js_strings.asp) – jonny Aug 21 '15 at 15:06
  • 2
    This answers the question I posted. However, the solution I used was to create an object literal directly from the json instead of first turning it into a string by simply removing the outer single quotes. Thanks to Quentin for pointing that out. – ezero Aug 21 '15 at 21:05
  • He's providing sample data. He's not actually writing out value literals. He probably wants something more global in nature, and not somewhere where he has to run every string value through an explicit escapeThisApostrophe(...) function, which would be horribly inefficient and dumb. – ChrisH Apr 27 '21 at 14:14
6

Just escape the single quote with a backslash such as \':

var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';

var parsed = JSON.parse(json);

//Output parsed to the document using JSON.stringify so it's human-readable and not just "[object Object]":
document.write(JSON.stringify(parsed));
Noble Mushtak
  • 1,784
  • 10
  • 22
1

Escape it with a backslash

var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';

var parsed = JSON.parse(json);
mechanicals
  • 685
  • 3
  • 14