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
  • 35,333
  • 28
  • 67
  • 92

3 Answers3

54

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
  • 98,751
  • 30
  • 222
  • 258
Vitalii Petrychuk
  • 13,757
  • 8
  • 50
  • 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
  • 259,174
  • 73
  • 529
  • 557
9

Try to Use JSON.stringify

Regards

grifos
  • 3,241
  • 1
  • 15
  • 14