JavaScript JSON

JSON.stringify and JSON.parse, including dropped keys, null substitutions, Date handling, and the exceptions each function throws.

モジュールページの本文は現在英語です。

JSON.stringify and JSON.parse are ECMAScript built-ins on the JSON object. They are not the browser Fetch API, not a Node.js module, and not Python’s json module.

The two functions do not share the same options or the same exceptions. stringify can throw TypeError. parse can throw SyntaxError. Do not expect parse to revive a Date object automatically.

Examples were checked in Node.js 22, which implements these ECMAScript built-ins. See the MDN documentation for the complete API.

JavaScript tutorial and API overview →

API index

APISummary
JSON.stringifySerialize a value to JSON text, applying the substitutions and omissions listed above.
JSON.parseParse a JSON string into a JavaScript value. Dates that were serialized as strings stay strings unless a reviver converts them.

JSON.stringify

JSON.stringify(value) converts a JavaScript value to a JSON string. Object properties whose value is undefined are omitted. Array holes and undefined array entries become null. NaN and Infinity also become null.

A Date is serialized as an ISO 8601 string. BigInt values and cyclic object graphs throw TypeError. That is not SyntaxError.

JSON.stringify

JSON.stringify(value, replacer, space)

Serialize a value to JSON text, applying the substitutions and omissions listed above.

ParameterDescription
valueThe JavaScript value to encode.
replacerOptional function or array of keys that can change or filter what is encoded.
spaceOptional number or string used to insert white space into the output. Omit it for compact text.

Returns: A JSON string, or undefined if the top-level value is undefined or a function.

Throws

  • TypeError — Thrown for BigInt values and for cyclic object graphs. JSON.parse does not throw this for invalid text.
  • If you stringify a Date and later parse the result, you get a string, not a Date object.
  • A replacer can encode values that the default serializer rejects, if you decide how they should appear as JSON.
javascript
console.log(JSON.stringify({ a: 1, b: undefined, c: null }));
console.log(JSON.stringify([1, undefined, null]));
console.log(JSON.stringify({ n: NaN, i: Infinity }));
console.log(JSON.stringify({ d: new Date("2026-11-22T00:00:00.000Z") }));
try { JSON.stringify({ x: 1n }); } catch (e) { console.log(e.name); }
const cyclic = {};
cyclic.self = cyclic;
try { JSON.stringify(cyclic); } catch (e) { console.log(e.name); }

Expected output

{"a":1,"c":null}
[1,null,null]
{"n":null,"i":null}
{"d":"2026-11-22T00:00:00.000Z"}
TypeError
TypeError

Related: JSON Diff tool, JSON.parse

MDN documentation →

JSON.parse

JSON.parse(text) reads JSON text and returns a JavaScript value. Invalid JSON throws SyntaxError. There is no sort option and no indent option on parse; those belong to stringify, if you use them at all.

JSON.parse

JSON.parse(text, reviver)

Parse a JSON string into a JavaScript value. Dates that were serialized as strings stay strings unless a reviver converts them.

ParameterDescription
textA string containing a JSON document.
reviverOptional function that can transform each produced value, including converting date strings back to Date objects if you write that logic yourself.

Returns: The corresponding JavaScript value.

Throws

  • SyntaxError — Thrown when the text is not valid JSON, for example unquoted keys. stringify does not throw SyntaxError for BigInt or cycles.
javascript
console.log(JSON.stringify(JSON.parse('{"port":80,"host":null}')));
const raw = JSON.parse('"2026-11-22T00:00:00.000Z"');
console.log(typeof raw, raw);
try { JSON.parse("{port:1}"); } catch (e) { console.log(e.name); }

Expected output

{"port":80,"host":null}
string 2026-11-22T00:00:00.000Z
SyntaxError

Related: JSON.stringify

MDN documentation →