JSON.stringify()

  • Revision slug: JavaScript/Reference/Global_Objects/JSON/stringify
  • Revision title: stringify
  • Revision id: 69013
  • Created:
  • Creator: Waldo
  • Is current revision? No
  • Comment add info about how toJSON methods are called to customize stringification behavior; 99 words added

Revision Content

Summary

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.

Method of JSON
Implemented in JavaScript 1.7
ECMAScript Edition ECMAScript 5th Edition

Syntax

var str = JSON.stringify(value[, replacer [, space]]);

Parameters

value
The value to convert to a JSON string.
replacer 
If a function, transforms values and properties encountered while stringifying; if an array, specifies the set of properties included in objects in the final string.
space 
Causes the resulting string to be pretty-printed.

Description

JSON.stringify converts an object to JSON notation representing it.

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}'

Properties of non-array objects are not guaranteed to be stringified in any particular order. Do not rely on ordering of properties within the same object within the stringification.

Boolean, Number, and String objects are converted to the corresponding primitive values during stringification, in accord with the traditional conversion semantics.

If undefined, a function, or an XML value is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array).

The space argument may be used to control spacing in the final string. If it is a number, successive levels in the stringification will each be indented by this many space characters (up to 10). If it is a string, successive levels will indented by this string (or the first ten characters of it).

print(JSON.stringify({ a: 2 }, null, " ")); // '{\n "a": 2\n}'

If an object being stringified has a property named toJSON whose value is a function, then the toJSON method customizes JSON stringification behavior: instead of the object being serialized, the value returned by the toJSON method when called will be serialized. For example:

var x = {};
x.foo = "foo";
x.toJSON = function() { return "bar"; };
var obj = { x: x };
var json1 = JSON.stringify(x);

json1 will be the string '{"x":"bar"}'.

Revision Source

<h3 name="Summary">Summary</h3>
<p>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.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/JSON" title="en/JavaScript/Reference/Global_Objects/JSON"><code>JSON</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript 1.7</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 5th Edition</td> </tr> </tbody>
</table>
<h3 name="Syntax">Syntax</h3>
<pre class="eval">var str = JSON.stringify(<em>value</em>[, <em>replacer</em> [, <em>space</em>]]);
</pre>
<h3 name="Parameters">Parameters</h3>
<dl> <dt><code>value</code></dt> <dd>The value to convert to a JSON string.</dd> <dt><code>replacer</code> </dt> <dd>If a function, transforms values and properties encountered while stringifying; if an array, specifies the set of properties included in objects in the final string.</dd> <dt><code>space</code> </dt> <dd>Causes the resulting string to be pretty-printed.</dd>
</dl>
<h3>Description</h3>
<p><code>JSON.stringify</code> converts an object to JSON notation representing it.</p>
<pre class="eval">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}'</pre>
<p>Properties of non-array objects are not guaranteed to be stringified in any particular order. Do not rely on ordering of properties within the same object within the stringification.</p>
<p>Boolean, Number, and String objects are converted to the corresponding primitive values during stringification, in accord with the traditional conversion semantics.</p>
<p>If <code>undefined</code>, a function, or an XML value is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array).</p>
<p>The space argument may be used to control spacing in the final string. If it is a number, successive levels in the stringification will each be indented by this many space characters (up to 10). If it is a string, successive levels will indented by this string (or the first ten characters of it).</p>
<pre class="eval">print(JSON.stringify({ a: 2 }, null, " ")); // '{\n "a": 2\n}'
</pre>
<p>If an object being stringified has a property named <code>toJSON</code> whose value is a function, then the <code>toJSON</code> method customizes JSON stringification behavior: instead of the object being serialized, the value returned by the <code>toJSON</code> method when called will be serialized. For example:</p>
<pre class="brush: js">var x = {};
x.foo = "foo";
x.toJSON = function() { return "bar"; };
var obj = { x: x };
var json1 = JSON.stringify(x);
</pre>
<p><code>json1</code> will be the string <code>'{"x":"bar"}'</code>.</p>
Revert to this revision