Skip to content

JSON

The JSON global object is a namespace that converts JavaScript values to JSON text and parses JSON text back into values. It can’t be constructed and only holds static members. JSON.stringify() turns objects, arrays, strings, numbers, Booleans, and null into a string, and JSON.parse() rebuilds them from one:

Language: JavaScript
> const order = { id: 42, items: ["mug", "tee"], paid: true };
undefined

> JSON.stringify(order)
'{"id":42,"items":["mug","tee"],"paid":true}'
> JSON.parse('{"id":42,"paid":true}')
{ id: 42, paid: true }

JSON is a text format with a smaller grammar than JavaScript. It has no undefined, functions, comments, single-quoted strings, or trailing commas, and every property name is a double-quoted string. The format itself is defined in ECMA-404, and JSON has been part of ECMAScript since ES5.

Like Math, JSON isn’t a constructor, so using it with new or calling it throws a TypeError.

Key Static Methods and Properties

Member Kind Description
JSON.parse() Static method Parses a JSON string and returns the value, optionally passing each key and value through a reviver function
JSON.stringify() Static method Returns a JSON string for a value, with an optional replacer that filters or transforms properties and an optional indent
JSON.rawJSON() Static method Wraps a piece of JSON text for a primitive so that JSON.stringify() inserts it unchanged (ES2026)
JSON.isRawJSON() Static method Returns whether a value is an object created by JSON.rawJSON() (ES2026)

JSON.parse() throws a SyntaxError for invalid JSON. A reviver receives a third context argument whose source property holds the original text of a primitive value. That argument, JSON.rawJSON(), and JSON.isRawJSON() were added in ES2026 and are Baseline Newly available since 2025.

Examples

An app saves a user’s editor settings to a file and wants the output readable. The third argument to JSON.stringify() sets the indent, and an array as the second argument keeps only the listed properties:

Language: JavaScript
> const settings = { theme: "dark", fontSize: 14 };
undefined

> console.log(JSON.stringify(settings, null, 2));
{
  "theme": "dark",
  "fontSize": 14
}
undefined
> JSON.stringify(settings, ["theme"])
'{"theme":"dark"}'

An API returns a timestamp, and the client needs it back as a Date. JSON.stringify() calls a value’s .toJSON() method, so a Date becomes an ISO string. A reviver function turns that string into a Date again while parsing:

Language: JavaScript
> JSON.stringify({ createdAt: new Date(Date.UTC(2026, 10, 12)) })
'{"createdAt":"2026-11-12T00:00:00.000Z"}'

> JSON.parse(
…   '{"createdAt":"2026-11-12T00:00:00.000Z"}',
…   (key, value) => (key === "createdAt" ? new Date(value) : value),
… )
{ createdAt: 2026-11-12T00:00:00.000Z }

A payment service sends order IDs larger than Number.MAX_SAFE_INTEGER, which a plain parse rounds. The reviver’s context.source keeps the exact digits for a BigInt, and JSON.rawJSON() writes them back without rounding:

Language: JavaScript
> JSON.parse('{"orderId": 12345678901234567890}')
{ orderId: 12345678901234567000 }
> JSON.parse(
…   '{"orderId": 12345678901234567890}',
…   (key, value, context) =>
…     key === "orderId" ? BigInt(context.source) : value,
… )
{ orderId: 12345678901234567890n }

> JSON.stringify({ orderId: JSON.rawJSON("12345678901234567890") })
'{"orderId":12345678901234567890}'

Common Gotchas

JSON.stringify() silently drops or replaces values it can’t represent. undefined, functions, and symbols disappear from objects and become null in arrays, and NaN and Infinity become null:

Language: JavaScript
> JSON.stringify({ name: "Ada", nickname: undefined, tags: [undefined, NaN] })
'{"name":"Ada","tags":[null,null]}'

Only enumerable own properties are serialized, so a Map or Set turns into an empty object:

Language: JavaScript
> JSON.stringify({ tags: new Set(["js"]), prices: new Map([["mug", 12]]) })
'{"tags":{},"prices":{}}'

A BigInt value or an object that refers to itself makes JSON.stringify() throw a TypeError:

Language: JavaScript
> JSON.stringify({ total: 10n })
Uncaught TypeError: Do not know how to serialize a BigInt
    at JSON.stringify (<anonymous>)

For a deep copy of data that holds a Map, a Date, or a cycle, the host-provided structuredClone() function handles what a JSON.stringify() and JSON.parse() round trip loses.

A smiling developer presses SET on a console beside a MAP cabinet of tagged drawers, while a KEY tag rides a conveyor in from an OBJECT tray of property cards.

Course

From Dicts to JavaScript Objects for Pythonistas (Fast Track)

Port a Python label script to JavaScript while writing object literals, filling defaults with ??, reaching into nested data with ?., removing duplicates with a Set, and reading and writing JSON.

basics js py

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 12, 2026