JSON.stringify: Undefined, BigInt, Dates and Circular References
JSON.stringify looks simple, but it silently drops some values, changes others and throws on a few. Most “where did my field go?” bugs come from the rules below. Every output here was produced with Node.js 24; browsers behave the same.
Quick reference
| Value | In an object | In an array |
|---|---|---|
undefined, functions, symbols | key is removed | becomes null |
NaN, Infinity | null | null |
Date | ISO string in UTC (via its toJSON method) | |
Map, Set | {} – the contents are lost | |
BigInt | throws TypeError | |
| circular reference | throws TypeError | |
Values that disappear
JSON.stringify({ a: undefined, b: () => 1, c: Symbol('x'), d: 1 })
// '{"d":1}'
JSON.stringify([undefined, () => 1, Symbol('x')])
// '[null,null,null]' – arrays keep their length
JSON.stringify({ n: NaN, i: Infinity, z: -0 })
// '{"n":null,"i":null,"z":0}'
JSON.stringify(undefined)
// undefined – not the string "undefined"!The last one bites when you write localStorage.setItem('x', JSON.stringify(value)) with an undefined value: you store the text "undefined", and JSON.parse fails on it later. Need to keep “no value” in the data? Use null instead of undefined.
Dates become strings (and stay strings)
JSON.stringify({ at: new Date(Date.UTC(2026, 10, 20, 9, 30)) })
// '{"at":"2026-11-20T09:30:00.000Z"}'
typeof JSON.parse(JSON.stringify({ d: new Date() })).d
// 'string'The round trip gives you back a string, not a Date. Convert it back with a reviver, the second argument of JSON.parse:
const data = JSON.parse(text, (key, value) =>
key === 'at' ? new Date(value) : value
)
data.at instanceof Date // trueMore on time zones and formats in Dates in JSON.
“Do not know how to serialize a BigInt”
JSON.stringify({ big: 10n })
// TypeError: Do not know how to serialize a BigInt
// Fix: turn BigInts into strings with a replacer
JSON.stringify({ big: 12345678901234567890n }, (key, value) =>
typeof value === 'bigint' ? value.toString() : value
)
// '{"big":"12345678901234567890"}'JSON.parse('{"id": 12345678901234567890}').id gives 12345678901234567000. JavaScript numbers are exact only up to 253 (about 9 quadrillion), so APIs send large IDs (Twitter/X, Discord, database snowflakes) as strings. Keep them as strings.“Converting circular structure to JSON”
const a = { name: 'a' }
a.self = a
JSON.stringify(a)
// TypeError: Converting circular structure to JSONThis usually happens when you log a DOM node, an Express req/res, an Axios error or an ORM model. The best fix is to pick the fields you need ({ id: user.id, name: user.name }). For debugging, a replacer can cut the loop:
function safeStringify(value, space) {
const seen = new WeakSet()
return JSON.stringify(value, (key, v) => {
if (typeof v === 'object' && v !== null) {
if (seen.has(v)) return '[Circular]'
seen.add(v)
}
return v
}, space)
}
safeStringify(a) // '{"name":"a","self":"[Circular]"}'[Circular]. Fine for logs; don’t use it for data you need to restore.Maps, Sets and classes
JSON.stringify({ m: new Map([['a', 1]]), s: new Set([1]) })
// '{"m":{},"s":{}}'
JSON.stringify({ m: new Map([['a', 1]]) }, (k, v) =>
v instanceof Map ? Object.fromEntries(v) : v instanceof Set ? [...v] : v
)
// '{"m":{"a":1}}'
class P { #secret = 1; constructor() { this.x = 1 } get y() { return 2 } }
JSON.stringify(new P())
// '{"x":1}' – private fields and getters are skipped, the class is lostControl the output: replacer, space and toJSON
// keep only some keys (array replacer)
JSON.stringify({ id: 1, name: 'Ann', password: 'x' }, ['id', 'name'])
// '{"id":1,"name":"Ann"}'
// hide sensitive keys (function replacer)
JSON.stringify({ id: 1, password: 'x' }, (k, v) => (k === 'password' ? undefined : v))
// '{"id":1}'
// indentation: a number (max 10) or a string like '\t'
JSON.stringify({ a: [1, 2] }, null, 2)
// an object can decide how it is serialized
const price = { amount: 999, currency: 'EUR', toJSON() { return `${(this.amount / 100).toFixed(2)} ${this.currency}` } }
JSON.stringify({ price })
// '{"price":"9.99 EUR"}'Key order
JSON.stringify({ b: 1, a: 2, 2: 'x', 1: 'y' })
// '{"1":"y","2":"x","b":1,"a":2}'Integer-like keys come first in ascending order, then the other keys in insertion order. So don’t compare JSON strings to check whether two objects are equal – compare the data. The JSON Diff tool ignores key order and shows real changes.
Putting JSON inside HTML
JSON.stringify('He said "hi"\n</script>')
// '"He said \"hi\"\n</script>"'JSON.stringify does not escape </script>. If you embed JSON in a <script> tag on a server-rendered page, user content containing </script> can break out of it (XSS). Escape < as \u003c when embedding.Reading the result
Long one-line output is hard to read. Paste it into the JSON Formatter, or explore it as a tree in the JSON Viewer. For more ways to format JSON, see How to pretty print JSON.