JSON.stringify()

Converts a JavaScript value to a JSON string.

Since ES5 Spec ↗

Syntax

JSON.stringify(value, replacer, space)

Parameters

NameTypeRequiredDescription
value any Yes The value to convert to a JSON string.
replacer Function | Array No A function (key, value) to transform values, or an array of keys to include.
space string | number No Indentation: a number of spaces (max 10) or a string used for pretty-printing.

Returns

string | undefined — The JSON string, or undefined if the value is a function or undefined.

Throws

  • TypeError — the value contains a circular reference or a BigInt.

Examples

console.log(JSON.stringify({ a: 1, b: [2, 3] }));
Output
{"a":1,"b":[2,3]}
console.log(JSON.stringify({ a: 1 }, null, 2));
Output
{
  "a": 1
}

Notes

undefined, functions, and symbols are omitted from objects and become null in arrays. Throws on circular references and BigInt. A `toJSON()` method on a value customizes its serialization (e.g. Date returns an ISO string).

Browser & runtime support

EnvironmentSince version
chrome 4
firefox 3.5
safari 4
edge 12
node 0.10

See also