44

Possible Duplicate:
Convert JS object to JSON string

I have a JSON object in JS, and I would like to convert it to string. Is it a function for this?

Thanks in advance,

Community
  • 1
  • 1
Danny Fox
  • 38,659
  • 28
  • 68
  • 94

3 Answers3

56

JSON.stringify()

Convert a value to JSON, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
Vitalii Petrychuk
  • 14,035
  • 8
  • 51
  • 55
37

You can use the JSON stringify method.

JSON.stringify({x: 5, y: 6}); // '{"x":5,"y":6}' or '{"y":6,"x":5}'

There is pretty good support for this across the board when it comes to browsers, as shown on http://caniuse.com/#search=JSON. You will note, however, that versions of IE earlier than 8 do not support this functionality natively.

If you wish to cater to those users as well you will need a shim. Douglas Crockford has provided his own JSON Parser on github.

Sampson
  • 265,109
  • 74
  • 539
  • 565
9

Try to Use JSON.stringify

Regards

grifos
  • 3,321
  • 1
  • 16
  • 14