3

How do I convert a JS object to a JSON string?

var o = { name: "a", id:5};
var sz = //???
alert('The json will look like ' + sz);

I would like to do it natively if possible, or using jQuery if not.

outis
  • 75,655
  • 22
  • 151
  • 221
  • possible duplicate of [JS object to JSON string?](http://stackoverflow.com/questions/834030/js-object-to-json-string) – outis Dec 26 '11 at 10:41

1 Answers1

8

You are looking for the JSON.stringify method:

var sz = JSON.stringify(o);

This method is part of the ECMAScript 5 Standard, and almost every browser includes it.

For older browsers and IE < 8, you can include the json2.js file.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838