JSON Object

The JSON object contains methods for parsing JSON and converting values to JSON.

assert(JSON.stringify({}) === '{}');
assert(JSON.stringify(true) === 'true');
assert(JSON.stringify("foo") === '"foo"');
assert(JSON.stringify([1, "false", false]) === '[1,"false",false]');
assert(JSON.stringify({ x: 5 }) === '{"x":5}');
JSON.stringify({x: 5, y: 6}); // '{"x":5,"y":6}' or '{"y":6,"x":5}'

Return a JSON string corresponding to the specified value, optionally including only certain properties or replacing property values in a user- defined manner.

JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null

Parse a string as JSON, optionally transform the produced value and its properties, and return the value.

Browser Compatibility

Newer browsers support the JSON object natively. Well-known polyfills for the JSON object are JSON2 and JSON3 that will only define JSON.stringify and JSON.parse if they’re not already defined, leaving any browser native implementation intact.

  1. Mozilla Developer Network: JSON
  2. JSON.stringify method
  3. JSON.parse method
  4. Serializing to JSON in jQuery