JSON Errors

How do you stringify a JSON object with circular references in JavaScript?

Stringify objects with circular references using custom replacer functions or libraries. Circular references occur when object properties reference the object itself or create reference cycles. Standard JSON.stringify() throws "Converting circular structure to JSON" error. Use replacer function tracking seen objects: const seen = new WeakSet(); JSON.stringify(obj, (key, value) => { if (seen.has(value)) return; seen.add(value); return value; }). This omits circular references. Libraries like flatted handle circular references preserving structure: flatted.stringify(obj). The circular-json package also manages cycles. For debugging, use util.inspect in Node.js which handles circularity. Consider restructuring data to eliminate circular references when possible—they indicate design issues often. For serialization, break cycles by omitting problematic references or using unique identifiers instead of direct references. Validate non-circular JSON with our JSON Formatter at jsonconsole.com/json-formatter before transmission. Circular references are valid in JavaScript but incompatible with JSON specification. Use specialized serialization for circular structures or redesign data model for proper JSON compatibility.
Last updated: December 23, 2025

Still have questions?

Can't find the answer you're looking for? Please reach out to our support team.