36

I'm using `JSON.stringify? to stringify an object, but the quotes are not escaped? Am I misunderstanding that it's suppose to escape the quotes?

This is outputted into the template without any of the quotes being escaped:

{"console":{"free":false}}
1
  • 2
    It should escape any quotes, using the JSON rules for escaping, in the data. What data are you putting in? What JSON are you getting out? How does that differ from what you expect to see? Commented Mar 31, 2011 at 20:38

5 Answers 5

64

stringify the object twice does the trick

console.log(JSON.stringify(JSON.stringify({"console":{"free":false}})));
// "{\"console\":{\"free\":false}}"
Sign up to request clarification or add additional context in comments.

3 Comments

well yes, but it appends additional quotes around the data which isn't a useable format.
@Urasquirrel if you are using this in Postman then make sure you remove the quotes in the Body. "jsonfield" : {{variable}}
That's a nice trick. Does the job for some situations!
38

It doesn't escape characters, no, there's encodeURIComponent for that, and you can use them together, as in encodeURIComponent(JSON.stringify(obj))

3 Comments

Assuming that the data needs to be URI encoded. The question doesn't indicate that, and there are many encoding schemes.
It seems like an error to say - "It doesn't escape characters, no, there's encodeURIComponent for that" and combining them seems like an ERROR as well... If you use JSON.stringify('XXXXX"') - you get (XXXXX%22) ... If you use encodeURIComponent('XXXXX"') - you get (XXXXX%2522) Why would you do both? If you use both encodeURIComponent(JSON.stringify('XXXXX"')) - you get (%22XXXXX%2522%22) Seems like this is an error to suggest this and I can;t think of a usage for this.
Thank you so much!!! This is the perfect solution. I found some other answers on StackOverFlow that don't do the job. What I actually used was an escape function passed to Stringify that calls encodeURIComponent
12

The quotes around property names are not supposed to be escaped, only quotes inside strings. Your JSON is fine :)

Comments

7

Without the offending code to inspect, I'm wondering if something else is happening. As a test...

<div id="test"/>

var ex = {'test':'This is "text".'};

$('#test').text(JSON.stringify(ex));

Outputs: {"test":"This is \"text\"."} (< Note the escaped double quotes)

http://jsfiddle.net/userdude/YVGbH/

1 Comment

And what makes us so sure that this $ exists in OP's environment and works the way it's described in the example? We can even ask the same question for DOM.
1

This is a bit old but here is my solution

const data = [{"name":"Mechanical2244","description":"Adjustment something..."},{"name":"Electricity","description":"Adjustment something2..."}];
const string = JSON.stringify(data)
        
console.log(string.replace(/"/g, '\\"'));

result

[{\"name\":\"Mechanical2244\",\"description\":\"Adjustment something...\"},{\"name\":\"Electricity\",\"description\":\"Adjustment something2...\"}]

1 Comment

This is the correct answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.