Converting circular structure to JSON
Converting circular structure to JSON
--> starting at object with constructor 'Object'
--- property 'self' closes the circleUnlike the other errors on this page, this one comes from JSON.stringify() rather than JSON.parse(). It occurs when the object graph you are serializing contains a reference cycle — an object that directly or indirectly refers back to itself. JSON is a tree format and cannot represent cycles, so the serializer detects the loop and throws rather than hanging in an infinite loop. Node's V8 gives a detailed message showing which property closes the cycle.
Object referencing itself
The most direct trigger: assigning an object as a property of itself creates a cycle.
Invalid
const obj = {}
obj.self = obj
JSON.stringify(obj) // throwsValid
const obj = { name: "Alice" }
JSON.stringify(obj) // safeParent-child back-reference in a tree
DOM-like tree structures where child nodes hold a reference to their parent create cycles. When serializing such a structure, the serializer detects the cycle through the parent reference.
Invalid
const parent = { name: "root", children: [] }
const child = { name: "leaf", parent }
parent.children.push(child)
JSON.stringify(parent) // cycle: parent → child.parent → parentValid
// Omit back-references during serialization
const { parent: _, ...safeChild } = child
JSON.stringify({ ...parent, children: [safeChild] })Express request / response objects
Express req and res objects contain many circular references internally. Attempting to serialize them directly with JSON.stringify in a log statement is a frequent mistake.
Invalid
console.log(JSON.stringify(req)) // req has circular refs
Valid
console.log(JSON.stringify({ url: req.url, method: req.method, body: req.body }))React component instances or DOM elements
DOM nodes and React fiber objects contain circular references by design. Serialize only the data you need, never the component or element itself.
Invalid
const el = document.getElementById("app")
JSON.stringify(el) // DOM nodes are circularValid
JSON.stringify({ id: el.id, className: el.className })Fix this error in seconds
Paste your JSON in the free validator to find the exact line and column where the error occurs.
Open JSON Validator →Frequently Asked Questions
How can I serialize an object that has circular references?
Use a replacer function that tracks visited objects: JSON.stringify(obj, getCircularReplacer()) where getCircularReplacer returns a function that replaces already-seen objects with undefined or a placeholder. Alternatively, use the json-stringify-safe or flatted npm packages which handle cycles automatically.
Can I detect circular references before calling JSON.stringify?
Yes. You can write a depth-first traversal that tracks a Set of visited objects and returns true if it finds a reference it has already seen. This is useful in debugging pipelines to catch circular data before it reaches a serialization boundary.