JSON
JSON is a text format for objects, arrays, strings, numbers, booleans, and null. JSON.stringify turns a JavaScript value into that text; JSON.parse turns the text back into values.
Detailed Explanation
JSON stands for JavaScript Object Notation, but it is not JavaScript. It is a strict subset: double-quoted keys and strings, no comments, no trailing commas, no undefined, no functions, no NaN or Infinity, no Date objects. A JSON document is data. A JavaScript object is a live heap value that can do anything.
You reach for JSON when two systems need to exchange a tree of data: a browser and an HTTP API, a file on disk, a message on a queue. Content-Type: application/json is the usual header. fetch plus response.json() is the usual browser path; that method parses the body and throws if the text is not valid JSON.
JSON.stringify(value) walks the value and builds a string. Objects become {...}, arrays become [...], strings are escaped, numbers are decimal. The walk is shallow in one sense and deep in another: it recurses into nested objects, but it only keeps JSON-safe types. undefined, functions, and symbols on objects are dropped. In arrays they become null. Date becomes an ISO string via toJSON. BigInt throws. A structure with a cycle throws TypeError: Converting circular structure to JSON.
JSON.stringify takes two optional arguments. A replacer function (or an array of keys) can filter or transform values. A space argument pretty-prints with indentation. Pretty JSON is for humans; minified JSON is for the wire.
JSON.parse(text) does the reverse. Invalid text throws SyntaxError. A optional reviver function is called for every key/value pair on the way out, which is how you turn ISO date strings back into Date objects if you choose to.
JSON is not a clone. JSON.parse(JSON.stringify(x)) is a common deep-copy hack. It drops functions, undefined, class identity, Map, Set, and Date (unless you revive them). Prefer structuredClone when you want a real structured clone.
What it costs you: size and fidelity. Numbers larger than Number.MAX_SAFE_INTEGER lose precision because JSON numbers are parsed as IEEE-754 doubles. Send those as strings, or use BigInt on both ends with a custom parse. Trusting JSON.parse on untrusted text is fine; trusting the shape of the result is not. Validate what you got.
Code Examples
const user = {
id: 7,
email: "ada@example.com",
roles: ["admin"],
createdAt: new Date("2026-01-15T00:00:00.000Z"),
passwordHash: undefined, // dropped
};
const text = JSON.stringify(user);
console.log(text);
// => {"id":7,"email":"ada@example.com","roles":["admin"],"createdAt":"2026-01-15T00:00:00.000Z"}
const back = JSON.parse(text);
console.log(back.createdAt instanceof Date); // => false (it is a string)
console.log(typeof back.createdAt); // => stringconst revived = JSON.parse(text, (key, value) => {
if (key === "createdAt") return new Date(value);
return value;
});
console.log(revived.createdAt instanceof Date); // => trueconst cycle = { name: "root" };
cycle.self = cycle;
try {
JSON.stringify(cycle);
} catch (err) {
console.log(err.name); // => TypeError
}
console.log(JSON.stringify([1, undefined, () => 0])); // => [1,null,null]
console.log(JSON.parse("not json").toString); // throws before this lineCommon Gotchas
Circular objects throw on stringify
break the cycle, omit the back-edge in a `replacer`, or use a format that supports references.
`JSON.parse(JSON.stringify(x))` is not a deep clone
`Date` becomes a string, `Map`/`Set` become `{}`, functions vanish, `BigInt` throws.
Integers above 2^53 − 1 are not safe as JSON numbers
they round-trip as IEEE doubles. Send them as strings if they are ids.
`undefined` is not JSON
on an object property it is omitted; in an array it becomes `null`. A top-level `undefined` makes `stringify` return `undefined`, not a string.