Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.stringify escaping without need

JSON.stringify is converting my json object to the following string

{\"2003\":{\"1\":{\"2\":[\"test\"],\"3\":[\"test2\"]}}}

When it should not be escaped. The result should be as the string quoted below

{"2003":{"1":{"2":["test"],"3":["test2"]}}}

Rather than use a general replace of all the escaped quotes and remove ones that could be in the input. How can I set JSON.stringify to not double escape the variables?

like image 813
John Avatar asked Jun 10 '12 07:06

John


People also ask

How do I remove an escape character from JSON Stringify?

If you simply want to replace the one special character, use: JSON. stringify({ a: 1, b: 2 }, null, '\t');

Does JSON Stringify escape quotes?

JSON. stringify does not act like an "identity" function when called on data that has already been converted to JSON. By design, it will escape quote marks, backslashes, etc. You need to call JSON.

Does JSON need to be escaped?

JSON is pretty liberal: The only characters you must escape are \ , " , and control codes (anything less than U+0020).

Does JSON Stringify have a limit?

So as it stands, a single node process can keep no more than 1.9 GB of JavaScript code, objects, strings, etc combined. That means the maximum length of a string is under 1.9 GB. You can get around this by using Buffer s, which store data outside of the V8 heap (but still in your process's heap).


1 Answers

You are stringifying a string, not an object:

var str = '{"2003":{"1":{"2":["test"],"3":["test2"]}}}'; var obj = {"2003":{"1":{"2":["test"],"3":["test2"]}}};  console.log( JSON.stringify(str) );  // {\"2003\":{\"1\":{\"2\":[\"test\"],\"3\":[\"test2\"]}}}  console.log( JSON.stringify(obj) );  // {"2003":{"1":{"2":["test"],"3":["test2"]}}}  
like image 179
Engineer Avatar answered Sep 22 '22 19:09

Engineer