Lingua-e
← Errors
JavaScript

JSON Circular Reference: What It Means and How to Fix It

Error reference

JSON Circular Reference

Language

JavaScript

Severity

Medium

When it happens

JSON.stringify() throws a TypeError when the object graph contains a circular reference, meaning an object refers back to itself (directly or indirectly).

Potential fixes

Use a replacer function in JSON.stringify() to detect and break cycles. Libraries like flatted or json-stringify-safe handle circular references automatically. Consider using structuredClone() for deep copying instead of JSON round-tripping.

Deep dive

JSON.stringify() throws a TypeError when the object graph contains a circular reference, meaning an object refers back to itself (directly or indirectly). JSON is a tree format and cannot represent cycles.

Why it happens

  • •An object has a property that points back to itself or to an ancestor in the graph.
  • •DOM nodes often have circular references (parentNode, ownerDocument).
  • •Express request/response objects contain circular references.
  • •A data model with bidirectional relationships (parent referencing child and child referencing parent).

Potential fixes

Use a replacer function in JSON.stringify() to detect and break cycles. Libraries like flatted or json-stringify-safe handle circular references automatically. Consider using structuredClone() for deep copying instead of JSON round-tripping.

Examples

JavaScript

Code that triggers the error

const a = {};
const b = { ref: a };
a.ref = b; // circular

JSON.stringify(a);

Error output

TypeError: Converting circular structure to JSON

Fixed code

function safeStringify(obj) {
  const seen = new WeakSet();
  return JSON.stringify(obj, (key, value) => {
    if (typeof value === "object" && value !== null) {
      if (seen.has(value)) return "[Circular]";
      seen.add(value);
    }
    return value;
  });
}
Practice in English

How would you explain a JSON Circular Reference to a fellow dev? Choose the right phrase:

"I hit an error...

For developers

Ready to practice your English at work?

Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.

Try Lingua-e for free →

No credit card needed

Vocabulary
merge conflictnoun

when two branches edit the same lines

Fill the gap

The build failedbecause of a missing dependency.

Multiple choice

What do you say when you're blocked?

AI'm blocked on this ticket.
BI am block on ticket.