Skip to content

TypeError: Converting circular structure to JSON

Error message

                Converting circular structure to JSON
              

Quick summary

Your object contains a circular reference (a property that points back to the object itself), which JSON.stringify cannot handle because it would loop forever.

Why this happens

  • An object has a property that references itself directly or indirectly

  • Two objects reference each other, creating a cycle

  • A DOM node is being serialized (DOM nodes have circular references)

Minimal example

✗ Broken code
const obj = { name: "Ada" };
obj.self = obj;
JSON.stringify(obj);
✓ Fixed code
const obj = { name: "Ada" };
// Don't create circular reference
JSON.stringify(obj);

obj.self points to obj itself, creating a cycle. Removing the self-reference allows JSON.stringify to work.

How to diagnose

  • Inspect the object for properties that reference the object itself

  • Check for mutual references between objects

  • Look for DOM nodes or other objects with built-in circular references

How to fix

  • Remove the circular reference before serializing

  • Use a custom replacer function in JSON.stringify to skip circular properties

  • Use a library like flatted or cycle.js that handles circular references

How to prevent

  • Avoid creating circular references in data that will be serialized

  • Use custom toJSON() methods to control what gets serialized

Related Resources

Related Glossary

Related Lessons

  • Objects

    Learn about object references.

Related Tools

← Back to language