JSON
JavaScript · Reference cheat sheet
JSON
JavaScript · Reference cheat sheet
📋 Overview
JSON is a text data format. JSON.parse and JSON.stringify convert between strings and plain data (objects, arrays, numbers, strings, booleans, null). Functions, undefined, Symbols, and many class instances need custom handling.
🔧 Core concepts
JSON.parse(text, reviver?): string → value; throwsSyntaxErroron bad input.JSON.stringify(value, replacer?, space?): value → string; omitsundefinedin objects.- Reviver / replacer: transform keys during parse/stringify.
toJSON(): objects can customize serialization.- Safe parse: wrap in try/catch; validate shape after parse.
const obj = JSON.parse('{"a":1}');
const text = JSON.stringify(obj, null, 2);💡 Examples
// Pretty print
JSON.stringify({ user: "Ada", ok: true }, null, 2);
// Replacer allowlist
JSON.stringify(user, ["id", "name"]);
// Reviver for dates
const parsed = JSON.parse(payload, (key, value) => {
if (key.endsWith("At") && typeof value === "string") {
const d = new Date(value);
if (!Number.isNaN(d.getTime())) return d;
}
return value;
});
// toJSON
const money = {
amount: 10,
currency: "USD",
toJSON() {
return `${this.amount} ${this.currency}`;
},
};
JSON.stringify({ price: money }); // {"price":"10 USD"}
// Map ↔ JSON
function mapToJson(map) {
return JSON.stringify([...map.entries()]);
}
function jsonToMap(text) {
return new Map(JSON.parse(text));
}
// Deep clone via JSON (limitations!)
const clone = JSON.parse(JSON.stringify(data));function safeParse(text, fallback = null) {
try {
return JSON.parse(text);
} catch {
return fallback;
}
}⚠️ Pitfalls
JSON.stringifydropsundefined, functions, and Symbols in objects; turns them tonullin arrays forundefined/functions.NaN/Infinitybecomenull.Datebecomes ISO string;Map/Setbecome\{\}unless converted.- Circular structures throw
TypeError. - JSON clone loses prototypes, methods, and
undefinedkeys.
🔗 Related
- objects.md — plain objects
- arrays.md — JSON arrays
- fetch.md — JSON HTTP bodies
- date.md — Date ↔ ISO
- map.md — serializing Maps
- error.md — SyntaxError on parse
- strings.md — text handling