34

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}}
Anders
  • 8,307
  • 9
  • 56
  • 88
Harry
  • 52,711
  • 71
  • 177
  • 261
  • 1
    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? – Quentin Mar 31 '11 at 20:38

5 Answers5

57

stringify the object twice does the trick

console.log(JSON.stringify(JSON.stringify({"console":{"free":false}})));
// "{\"console\":{\"free\":false}}"
Nerdroid
  • 13,398
  • 5
  • 58
  • 69
38

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

Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
  • 8
    Assuming that the data needs to be URI encoded. The question doesn't indicate that, and there are many encoding schemes. – Quentin Mar 31 '11 at 20:38
  • 2
    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. –  Jan 29 '13 at 23:54
  • 2
    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 – Mitzi Jun 18 '13 at 12:26
11

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

Ricardo Tomasi
  • 34,573
  • 2
  • 55
  • 66
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/

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
  • 3
    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. – AlicanC Dec 04 '14 at 14:48
0

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...\"}]
Johansrk
  • 5,160
  • 3
  • 38
  • 37