JSON.parse gives exception with newline character in string
<p>I'm getting the following string:</p>

<p><code>var str='{"message":"hello\nworld"}';</code></p>

<p>I need to turn it into JSON object. However, I get an exception when I try <code>JSON.parse(str)</code> because of the <code>\n</code></p>

<p>I saw <a href="https://stackoverflow.com/questions/29666036/json-parse-with-newline">this</a> question but it did not help.</p>

<p>From that, I tried
<code>var j=JSON.parse(JSON.stringify(str))</code></p>

<p>But I'm still getting string instead of object when i use <code>typeof j</code></p>

<p>I know using <code>\\n</code> works, but the thing is, it does not print on new line when i need to use the value.</p>

<p><strong>UPDATE:</strong> OK, i just realized <code>\\n</code> is working.
I'm using this to convert <code>\n</code> to <code>\\n</code>:</p>

<pre><code>var str='{"message":"hello\nworld"}';
str=str.replace(/\n/g, "\\\\n").replace(/\r/g, "\\\\r").replace(/\t/g, "\\\\t");

var json=JSON.parse(str);

console.log(json.message);
</code></pre>

<p>Can someone please correct it?</p>