← Guides

Developer Guide

JSON Circular Reference Error

Fix "Converting circular structure to JSON" error. Learn what causes circular references and how to serialize objects that reference themselves.

circular reference jsonconverting circular structurejson stringify errorcircular object jsonjson serialization error

The "Converting circular structure to JSON" error occurs when you try to JSON.stringify() an object that references itself. JSON cannot represent circular references.

What is a circular reference?

A circular reference occurs when an object references itself directly or through a chain: const obj = {}; obj.self = obj; This creates an infinite loop that JSON.stringify cannot handle.

Common causes

DOM elements have circular references (parentNode, childNodes). Error objects and certain library objects may also have cycles. Debugging: console.log(obj) works, but JSON.stringify(obj) fails.

Solution: custom replacer

Pass a replacer function to JSON.stringify() that detects and skips circular references. Example: const seen = new Set(); JSON.stringify(obj, (key, value) => { if (typeof value === "object" && value !== null) { if (seen.has(value)) return "[Circular]"; seen.add(value); } return value; });

Solution: remove the cycle

If you control the data structure, remove the circular property before serializing, or create a plain object with only the fields you need.

Summary

JSON cannot express circular references. Use a custom replacer or restructure your data. For debugging, console.log() handles cycles that JSON.stringify() cannot.

Try it yourself

Put what you've learned into practice with our free browser-native tool.

Open Tool →