Full answer
Parsing
- JSON.parse(text) throws SyntaxError for invalid JSON (trailing commas, single quotes, comments, undefined). Parse untrusted text inside try/catch and validate the result (zod, ajv).
- The reviver function (key, value) lets you convert values, for example ISO date strings back to Date objects.
- JSON.parse does not cause prototype pollution on its own: a 'proto' key becomes an own property. Danger appears when you merge the result into other objects recursively.
- Large JSON blocks the event loop, because parsing is synchronous. Use streaming or NDJSON for very large data.
Stringifying
- Date objects use toJSON and become ISO strings. They come back as strings.
- undefined, functions and symbols are omitted in objects and become null in arrays.
- NaN and Infinity become null.
- BigInt throws TypeError: Do not know how to serialize a BigInt. Convert to string (a replacer or BigInt.prototype.toJSON).
- Map, Set and class instances become {} or plain data. Convert them first.
- Circular references throw TypeError. Remove the cycle or use a replacer with a WeakSet. structuredClone copes with cycles but is not a JSON tool.
- toJSON() on your class controls the output. The replacer (array or function) filters or changes fields. The third argument adds indentation.
Numbers
- JSON numbers are parsed as IEEE double. An id like 9007199254740993 becomes 9007199254740992. Send large ids as strings.
- For money, use integers (cents) or strings, not floats.
Practical rules: never build JSON by string concatenation, set Content-Type: application/json, and limit the request body size.
// 1. Parse errors
for (const text of ["{'a': 1}", '{"a": 1,}', '', 'undefined']) {
try { JSON.parse(text); } catch (err) { console.log('1. parse', JSON.stringify(text).padEnd(11), '->', err.name); }
}
// 2. What stringify does to special values
const data = { date: new Date(Date.UTC(2026, 11, 4)), missing: undefined, run() {}, nan: NaN, inf: Infinity, list: [undefined, () => {}], set: new Set([1]), map: new Map([['a', 1]]) };
console.log('2.', JSON.stringify(data));
// 3. Dates come back as strings, unless a reviver converts them
const roundTrip = JSON.parse(JSON.stringify({ at: new Date(Date.UTC(2026, 11, 4)) }));
console.log('3. typeof after round trip:', typeof roundTrip.at);
const revived = JSON.parse(JSON.stringify({ at: new Date(Date.UTC(2026, 11, 4)) }), (key, value) => (key === 'at' ? new Date(value) : value));
console.log(' with a reviver:', revived.at instanceof Date);
// 4. BigInt and big numbers
try { JSON.stringify({ id: 12n }); } catch (err) { console.log('4. BigInt ->', err.name); }
console.log(' BigInt as string:', JSON.stringify({ id: 12n }, (key, value) => (typeof value === 'bigint' ? value.toString() : value)));
console.log(' 9007199254740993 parsed as', JSON.parse('{"id":9007199254740993}').id);
// 5. Circular data
const a = { name: 'a' }; a.self = a;
try { JSON.stringify(a); } catch (err) { console.log('5. circular ->', err.name); }
// 6. toJSON and a replacer that hides a secret
class User {
constructor() { this.name = 'Asha'; this.password = 'secret'; }
toJSON() { return { name: this.name }; }
}
console.log('6. toJSON:', JSON.stringify(new User()), '| replacer:', JSON.stringify({ a: 1, token: 'x' }, (k, v) => (k === 'token' ? undefined : v)));
// 7. __proto__ in parsed JSON is just data
const parsed = JSON.parse('{"__proto__": {"admin": true}}');
console.log('7. polluted a fresh object:', ({}).admin === true, '| own key:', Object.keys(parsed).join(','));Each numbered line shows one pitfall with real output. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): 1. parse "{'a': 1}" -> SyntaxError 1. parse "{\"a\": 1,}" -> SyntaxError 1. parse "" -> SyntaxError 1. parse "undefined" -> SyntaxError 2. {"date":"2026-12-04T00:00:00.000Z","nan":null,"inf":null,"list":[null,null],"set":{},"map":{}} 3. typeof after round trip: string with a reviver: true 4. BigInt -> TypeError BigInt as string: {"id":"12"} 9007199254740993 parsed as 9007199254740992 5. circular -> TypeError 6. toJSON: {"name":"Asha"} | replacer: {"a":1} 7. polluted a fresh object: false | own key: __proto__
Key points
- JSON.parse throws, so use try/catch and validate.
- Dates become strings, undefined is dropped, NaN becomes null.
- BigInt throws, and integers above 2^53 lose precision.
- Circular structures throw.
- toJSON, replacer and reviver control the format.
Common mistakes
- Passing user text to JSON.parse without try/catch.
- Sending 64-bit ids as JSON numbers.
- Expecting Date objects after a round trip.
- Logging objects with passwords through JSON.stringify without a filter.
- Building JSON strings by hand.
Follow-ups
- How would you serialise a Map or a Set?
- How does structuredClone differ from JSON for copying?
- How do you protect an API from huge JSON bodies?
Tips
- List what stringify drops or changes.
- Mention the 2^53 limit.
- Say 'try/catch and validate'.