title: How to use a JSON literal string?
body: <p>Since the JSON format specifies that single quotes should not be escaped, most libraries (or even the native JSON parser) will fail if you have an escaped single quote in it. Now this usually is not a problem since most of the time you do an XHR that fetches some data formatted as JSON and you use the responseText which contains your JSON string that you can then parse, etc.</p>
  
  <p>In this particular situation, I have a JSON string stored in a database as text... so the database contains something like <code>{"property":"value"}</code> and I want to output this as part of an HTML page created by the server so that the JavaScript code in that page looks something like this:</p>
  
  <pre><code>var x = '{"property":"value"}';
  </code></pre>
  
  <p>Now if the JSON string in the database contains a single quote like this:</p>
  
  <pre><code>{"property":"val'ue"}
  </code></pre>
  
  <p>Then I need to escape it or else I will never be able to use it as a string:</p>
  
  <pre><code>console.clear();
  var obj = {prop:"val'ue"};
  var str = JSON.stringify(obj);
  console.log("JSON string is %s",str);
  console.dir(JSON.parse(str)); //No problem here
  
  
  //This obviously can't work since the string is closed and it causes an invalid script
  //console.dir(JSON.parse('{prop:"val'ue"}'));
  
  //so I need to escape it to use a literal JSON string
  console.dir(JSON.parse('{"prop":"val\'ue"}'));
  </code></pre>
  
  <p>The question then is why <code>{"prop":"val\'ue"}</code> not considered a valid JSON string ?</p>