JSON Stringify: Common Errors and How to Fix Them
Even experienced JavaScript developers run into issues when using JSON Stringify. From circular references to unexpected undefined values, these errors can break apps, corrupt data, or cause silent failures. Understanding these common mistakes—and how to fix them—will save you hours of debugging and prevent production bugs.
In this guide, we'll walk through the most common JSON Stringify errors, explain why they happen, and show you practical solutions with clear examples.
TypeError: Converting Circular Structure to JSON
This is the most common JSON Stringify error. It occurs when an object references itself, either directly or through a chain of references.
What causes it?
const obj = { name: "Alex" }; obj.self = obj; JSON.stringify(obj); // TypeError: Converting circular structure to JSON
Here, obj.self points back to obj, creating a circular reference that JSON Stringify cannot serialize.
How to fix it
Option 1: Remove the circular reference manually
const obj = { name: "Alex" }; obj.self = obj; delete obj.self; console.log(JSON.stringify(obj)); // → {"name":"Alex"}
Option 2: Use a custom replacer to detect cycles
const seen = new WeakSet(); const safeStringify = (obj) => JSON.stringify(obj, (key, value) => { if (typeof value === "object" && value !== null) { if (seen.has(value)) return undefined; seen.add(value); } return value; }); const obj = { name: "Alex" }; obj.self = obj; console.log(safeStringify(obj)); // → {"name":"Alex"}
Option 3: Use a library like flatted
import { stringify } from "flatted"; const obj = { name: "Alex" }; obj.self = obj; console.log(stringify(obj));
Libraries like flatted handle circular structures automatically.
Must Read: What Is JSON Stringify? Complete Guide with Examples
TypeError: Do Not Know How to Serialize a BigInt
JSON does not support BigInt values, so JSON Stringify throws an error when it encounters one.
What causes it?
const data = { id: 123n }; JSON.stringify(data); // TypeError: Do not know how to serialize a BigInt
How to fix it
Option 1: Convert BigInt to a string manually
const data = { id: 123n }; const safe = JSON.stringify(data, (key, value) => typeof value === "bigint" ? value.toString() : value ); console.log(safe); // → {"id":"123"}
Option 2: Use a custom toJSON method
BigInt.prototype.toJSON = function () { return this.toString(); }; const data = { id: 123n }; console.log(JSON.stringify(data)); // → {"id":"123"}
Note that modifying built-in prototypes is generally discouraged in production code.
Unexpected Undefined or Missing Properties
JSON Stringify silently omits properties with undefined values, functions, and symbols.
What causes it?
const user = { name: "Alex", age: undefined, greet: () => "Hello", [Symbol("id")]: 123 }; console.log(JSON.stringify(user)); // → {"name":"Alex"}
All three unsupported values (undefined, function, symbol) are silently dropped.
How to fix it
Option 1: Convert undefined to null
const user = { name: "Alex", age: null // instead of undefined }; console.log(JSON.stringify(user)); // → {"name":"Alex","age":null}
Option 2: Use a replacer to handle undefined
const user = { name: "Alex", age: undefined }; const safe = JSON.stringify(user, (key, value) => value === undefined ? null : value ); console.log(safe); // → {"name":"Alex","age":null}
If you need the property to appear, convert undefined to null or a placeholder string.
Functions and Methods Are Ignored
JSON Stringify does not serialize functions because JSON is a data format, not a code format.
What causes it?
const obj = { name: "Product", calculate: function () { return this.price * 1.1; } }; console.log(JSON.stringify(obj)); // → {"name":"Product"}
The calculate function is silently omitted.
How to fix it
Store the function result as data instead:
const obj = { name: "Product", price: 100, total: function () { return this.price * 1.1; } }; const data = { name: obj.name, price: obj.price, total: obj.total() }; console.log(JSON.stringify(data)); // → {"name":"Product","price":100,"total":110}
If you need to transmit behavior, use a different mechanism like class names or function identifiers.
Date Objects Serialize to ISO Strings
Date objects are automatically converted to ISO 8601 strings during serialization.
What causes it?
const event = { name: "Launch", date: new Date("2025-12-31T23:59:59Z") }; console.log(JSON.stringify(event)); // → {"name":"Launch","date":"2025-12-31T23:59:59.000Z"}
This isn't an error, but it can be surprising if you expect a Date object back after parsing.
How to fix it
Option 1: Accept the ISO string and parse it later
const jsonStr = '{"date":"2025-12-31T23:59:59.000Z"}'; const parsed = JSON.parse(jsonStr, (key, value) => key === "date" ? new Date(value) : value ); console.log(parsed.date instanceof Date); // → true
Option 2: Use a custom toJSON method
class CustomDate extends Date { toJSON() { return this.toISOString().slice(0, 10); // YYYY-MM-DD } } const event = { name: "Launch", date: new CustomDate("2025-12-31T23:59:59Z") }; console.log(JSON.stringify(event)); // → {"name":"Launch","date":"2025-12-31"}
NaN, Infinity, and -Infinity Convert to null
Special numeric values like NaN, Infinity, and -Infinity are not valid JSON numbers, so they serialize as null.
What causes it?
const data = { value1: NaN, value2: Infinity, value3: -Infinity }; console.log(JSON.stringify(data)); // → {"value1":null,"value2":null,"value3":null}
How to fix it
Option 1: Replace special values before stringifying
const data = { value1: NaN, value2: Infinity, value3: -Infinity }; const safe = JSON.stringify(data, (key, value) => { if (typeof value === "number" && !isFinite(value)) { return String(value); } return value; }); console.log(safe); // → {"value1":"NaN","value2":"Infinity","value3":"-Infinity"}
Option 2: Validate input data
Check for these values before calling JSON Stringify and handle them explicitly in your application logic.
Map and Set Objects Serialize to Empty Objects
Maps and Sets do not have enumerable properties, so JSON Stringify outputs an empty object.
What causes it?
const map = new Map([["key", "value"]]); const set = new Set([1, 2, 3]); console.log(JSON.stringify(map)); // → {} console.log(JSON.stringify(set)); // → {}
How to fix it
Convert Maps and Sets to arrays before stringifying:
const map = new Map([["key", "value"]]); const set = new Set([1, 2, 3]); const data = { mapData: Array.from(map.entries()), setData: Array.from(set) }; console.log(JSON.stringify(data)); // → {"mapData":[["key","value"]],"setData":[1,2,3]}
When parsing back, reconstruct the Map or Set:
const parsed = JSON.parse(jsonStr); const restoredMap = new Map(parsed.mapData); const restoredSet = new Set(parsed.setData);
Forgetting to Stringify Before Sending to APIs
A common beginner mistake is passing a raw object instead of a JSON string to fetch or XHR.
What causes it?
// ❌ Wrong fetch("/api/users", { method: "POST", body: { name: "Alex" } });
This sends [object Object] as the body, which is not valid JSON.
How to fix it
Always stringify objects before sending them:
// ✅ Correct fetch("/api/users", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: "Alex" }) });
Best Practices to Avoid JSON Stringify Errors
- Always handle circular references before stringifying.
- Convert BigInt values to strings manually.
- Use null instead of undefined if you need the property to appear.
- Store function results as data, not functions themselves.
- Convert Maps and Sets to arrays before serialization.
- Validate and sanitize data before stringifying.
- Use try-catch when stringifying untrusted or complex data.
- Test round-trip behavior (stringify → parse) in your workflows.
Conclusion: Debug and Fix JSON Stringify Errors with Confidence
Understanding common JSON Stringify errors—and how to fix them—will make you a more confident JavaScript developer. Whether you're dealing with circular references, BigInt values, or unexpected undefined properties, these solutions will help you avoid bugs and write safer, more reliable code.
Tools on JSON Kithub help:
- Convert YAML to JSON
- Convert JSON to YAML
- Stringify JSON
- Parse JSON
- JSON formatter
- Compare JSON
- JSON Validator
- Minify JSON
- JSON Escape
- JSON Unescape
- Convert JSON to TOON
- Convert TOON to JSON
FAQs On JSON Stringify Common Errors
Q1. Why does JSON Stringify throw a circular structure error?
JSON Stringify throws this error when an object references itself, either directly or through nested properties. Remove or replace circular references before stringifying.
Q2. How do I fix "Do not know how to serialize a BigInt"?
Convert BigInt values to strings using a replacer function or by manually calling .toString() before stringifying.
Q3. Why are undefined properties missing from the JSON output?
JSON Stringify silently omits properties with undefined values because undefined is not a valid JSON type. Use null instead if you need the property to appear.
Q4. How do I serialize Date objects with JSON Stringify?
Date objects automatically serialize to ISO 8601 strings. You can define a custom toJSON method to control the format.
Q5. What happens to NaN and Infinity in JSON Stringify?
They are converted to null because they are not valid JSON numbers. Use a replacer function to convert them to strings if you need to preserve them.
Ready to Try Our JSON Tools?
Format, validate, and transform your JSON data with our free online tools.