Blogchevron_rightJSON Tools
JSON Tools

7 JSON.stringify() Pitfalls That Catch Developers Off Guard

JSON.stringify() looks simple but has a surprising number of edge cases. These seven pitfalls have caught many experienced developers off guard.

April 18, 2026·7 min read

Pitfall 1: Silent Omission of undefined

When an object property value is undefined, JSON.stringify omits it entirely rather than serializing it as null. This can cause your JSON to be missing expected keys, and the receiver has no way to know whether the key was intentionally absent or accidentally omitted.

Fix: explicitly set values to null for intentionally absent properties: { name: user.name ?? null }. Use a replacer that converts undefined to null if you need to preserve all keys: (key, value) => value === undefined ? null : value.

Pitfall 2: Date Objects Become Strings

JSON.stringify converts Date objects to ISO 8601 strings via toISOString(). After JSON.parse(), those strings are still strings — they are not automatically converted back to Date objects. Code that passes a Date to stringify and expects a Date back after parse will silently get a string.

Fix: handle date conversion explicitly. After parsing, convert date string fields to Date objects: new Date(data.createdAt). Alternatively, store timestamps as Unix epoch integers (numbers), which survive the round-trip without any conversion.

Pitfall 3: Circular References Throw

JSON.stringify throws TypeError: Converting circular structure to JSON when the object contains a circular reference — where an object's property references a parent object in its own chain. This is common with DOM nodes, some framework objects, and developer-constructed graphs.

Fix: use structuredClone() to create a clean copy before stringifying, or implement a custom replacer that breaks circular references. The flatted library provides JSON.stringify and JSON.parse replacements that handle circular structures.

Pitfall 4: Non-Enumerable and Prototype Properties Are Ignored

JSON.stringify only includes own enumerable properties. Properties defined on the prototype chain (including class methods) and non-enumerable properties (defined with Object.defineProperty without enumerable: true) are silently ignored.

This is usually the desired behavior but can surprise developers who expect all properties to serialize. If a class instance does not serialize as expected, check whether properties are defined on the prototype or as non-enumerable. Implement toJSON() to control serialization explicitly.

Try JSON Stringify Online Free Online

No sign-up required. 100% client-side — your data never leaves your browser.

Open JSON Stringify Onlinearrow_forward

Frequently Asked Questions

How do I detect if JSON.stringify is silently dropping properties?

Compare the key counts: if Object.keys(original).length !== Object.keys(JSON.parse(JSON.stringify(original))).length, keys were dropped. For nested objects, use a recursive comparison.

Does JSON.stringify handle BigInt?

No. JSON.stringify throws TypeError for BigInt values. Convert to string or number before stringifying: { id: myBigInt.toString() }.

Can JSON.stringify handle NaN and Infinity?

Both NaN and Infinity are converted to null by JSON.stringify, since JSON has no representation for these values. Be explicit: convert to null, a string, or 0 before stringifying.

7 JSON.stringify() Pitfalls That Catch Developers Off Guard