Fix “Converting Circular Structure to JSON”
JavaScript throws this error when JSON.stringify follows object references back to an ancestor. Find the cycle and serialize an explicit data shape instead of the whole runtime object.
Understand the unsupported shape
JSON contains objects, arrays, strings, numbers, booleans, and null arranged as a tree. It has no native object identity or pointer notation. A cycle occurs when an object eventually refers back to itself, directly or through other objects. Framework requests, responses, DOM nodes, sockets, errors, and parent-linked models commonly contain such references.
Locate the cycle
Read the property path included by modern runtimes when available. Inspect object keys selectively in a debugger without recursively expanding everything. Start from the value passed to JSON.stringify, not from the final API call. A direct cycle looks like value.self = value; an indirect cycle may follow child, owner, and parent links across several objects.
Prefer an explicit serializable projection
Build a plain object containing only the fields required by the destination. For an HTTP error log, that might be method, sanitized URL, status, request ID, and message rather than the entire request and response objects. For an API response, map domain entities to a documented transfer shape. This removes cycles, prevents accidental secret leakage, and keeps contracts stable when framework internals change.
Distinguish cycles from repeated references
The same object can appear in two sibling properties without forming a cycle. JSON can represent the duplicated values, but object identity is lost after parsing. A replacer that globally drops every previously seen object can incorrectly remove this legitimate repeated data. If a custom replacer is necessary, track the current ancestor chain rather than treating all repeated references as circular.
Use placeholders only for diagnostic output
For developer logs, a safe serializer can replace actual cycles with a marker such as "[Circular]". That output changes the data and should not silently become a business payload. Apply depth, size, key, and redaction limits so one object cannot flood logs or expose credentials, cookies, tokens, or personal data.
Model graph data deliberately
If identity and references are part of the domain, encode them explicitly with stable IDs and separate entity collections. A tree can include child IDs instead of embedded parent objects. Document how consumers resolve references and handle missing nodes. Do not invent an ad hoc pointer format inside a generic JSON endpoint without a contract.
Verify the output
Test direct cycles, indirect cycles, repeated non-circular references, deep input, and secret redaction. Validate the resulting string with the JSON Validator and inspect its shape using the JSON Tree Viewer.
Frequently Asked Questions
Why can JSON not contain cycles?
It has no built-in object identity or reference syntax.
Is every repeated reference circular?
No. Sibling reuse can be acyclic.
Should framework objects be stringified whole?
Usually no. Project only the required safe fields.