Fixing “Converting circular structure to JSON” in JavaScript
TypeError: Converting circular structure to JSON is thrown when the object passed to JSON.stringify contains a reference that leads back to the original object (a circular reference). It happens at a JSON.stringify call made before saving to localStorage or sending JSON with fetch. The message also shows the property that closes the loop, so you can either replace that reference with an ID or keep it out of the JSON, and the error goes away.
In my test environment, I triggered this error in both the browser and Node.js and copied the messages exactly as they appeared. After the fix, the error no longer occurred and the JSON was saved to localStorage, which I also confirmed.
The test environment was Ubuntu 24.04 and Node.js 22.23.4. I served the page with Python 3.11's http.server and opened it in Chromium 153 launched by Playwright 1.63.0. I retrieved console messages through the DevTools protocol and also took screenshots of the DevTools panel. I did not test Firefox or Safari, or use tools such as React or Vite.
- 1. A list and a task that point to each other cause the error
- 2. Lines 2 and below of the message show where the loop is
- 3. The same message appears in the Node.js terminal
- 4. Message differences between browsers
- 5. Give the child an ID instead of a reference back to the parent
- 6. Keep only the loop-forming property out of the JSON
- 7. Referencing the same object from two places does not cause an error if there is no loop
- 8. Replacing the cycle with a placeholder without knowing where it is
A list and a task that point to each other cause the error
Here, a task is added to a shopping list. The task has a list property that points to its list, so you can walk from the task back to the list. The code tries to convert that list to JSON and save it to localStorage.
const out = document.getElementById('out');
const list = { name: '買い物', tasks: [] };
const task = { title: '牛乳を買う', list };
list.tasks.push(task);
const json = JSON.stringify(list);
localStorage.setItem('list', json);
out.textContent = `保存しました:${json}`;
When the page loaded, the paragraph on the screen stayed at its "before saving" text, and the following appeared in the DevTools Console panel.
Uncaught TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
| property 'tasks' -> object with constructor 'Array'
| index 0 -> object with constructor 'Object'
--- property 'list' closes the circle
at JSON.stringify (<anonymous>)
at main.js:7:19

Because JSON.stringify threw an error, nothing was saved to localStorage (in the test, localStorage.getItem('list') returned null).
According to MDN's description of JSON.stringify(), JSON has no way to express a reference to an object, so passing a value that contains a circular reference throws a TypeError.
Lines 2 and below of the message show where the loop is
Lines 2 through 5 of the message show the path JSON.stringify followed through the object. In the message above, they read as follows.
starting at object with constructor 'Object': The loop starts atlist, the plain object passed toJSON.stringify.property 'tasks' -> object with constructor 'Array': It moves to the arraylist.tasks.index 0 -> object with constructor 'Object': It moves to element 0 of that array, which istask.property 'list' closes the circle:task.listpoints back to the originallist, which closes the loop.
In other words, the cause is that list.tasks[0].list refers to list itself. To fix it, look at the property named on the --- property 'list' closes the circle line (here, list).
When an object refers to itself directly (user.self = user), there are no intermediate lines. The message showed --- property 'self' closes the circle right below --> starting at object with constructor 'Object'.
For objects created from a class, the class name appears after constructor. When I built the same shape with classes named TaskList and Task, the message was as follows.
Uncaught TypeError: Converting circular structure to JSON
--> starting at object with constructor 'TaskList'
| property 'tasks' -> object with constructor 'Array'
| index 0 -> object with constructor 'Task'
--- property 'list' closes the circle
at JSON.stringify (<anonymous>)
at main.js:19:19
The last line, main.js:19:19, is line 19, column 19 of main.js. That is the position of stringify in const json = JSON.stringify(list);.
The same message appears in the Node.js terminal
I ran the same code in Node.js. I removed the localStorage line and added console.log(list) before JSON.stringify to print the object's contents.
const list = { name: '買い物', tasks: [] };
const task = { title: '牛乳を買う', list };
list.tasks.push(task);
console.log(list);
console.log(JSON.stringify(list));
The terminal printed the following, and the exit code was 1. (I replaced the file location with /path/to/cycle.js.)
<ref *1> {
name: '買い物',
tasks: [ { title: '牛乳を買う', list: [Circular *1] } ]
}
/path/to/cycle.js:6
console.log(JSON.stringify(list));
^
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Object'
| property 'tasks' -> object with constructor 'Array'
| index 0 -> object with constructor 'Object'
--- property 'list' closes the circle
at JSON.stringify (<anonymous>)
at Object.<anonymous> (/path/to/cycle.js:6:18)
at Module._compile (node:internal/modules/cjs/loader:1781:14)
at Object..js (node:internal/modules/cjs/loader:1913:10)
at Module.load (node:internal/modules/cjs/loader:1505:32)
at Function._load (node:internal/modules/cjs/loader:1309:12)
at wrapModuleLoad (node:internal/modules/cjs/loader:254:19)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5)
at node:internal/main/run_main_module:36:49
Node.js v22.23.4
The five lines from TypeError: Converting circular structure to JSON through closes the circle were the same as in the browser.
console.log did not throw. It marked the loop with [Circular *1]. As described in Node.js's util.inspect() documentation, an object marked <ref *1> is shown as [Circular *1] wherever it is reached again. Here you can see that the list inside tasks leads back to the outer list. If you only want to inspect the contents, printing with console.log without JSON.stringify is enough.
Message differences between browsers
According to MDN's explanation of "TypeError: cyclic object value", the message differs by browser even for the same cause. The messages I actually saw were Converting circular structure to JSON in Chromium and Node.js. The Firefox and Safari messages below are the ones listed on MDN.
| Environment | Message |
|---|---|
| Engines based on V8 (Chromium, Node.js, etc.) | TypeError: Converting circular structure to JSON |
| Firefox | TypeError: cyclic object value |
| Safari | TypeError: JSON.stringify cannot serialize cyclic structures. |
Give the child an ID instead of a reference back to the parent
If the data is going to be saved as JSON or sent somewhere, the simplest fix is to avoid creating the loop. Don't give the task the list object itself; give it the list's ID.
const list = { id: 'shopping', name: '買い物', tasks: [] };
const task = { title: '牛乳を買う', listId: list.id };
list.tasks.push(task);
In the browser, no error occurred and the Console panel stayed empty. The screen showed the following, and the same string was saved to localStorage.
保存しました:{"id":"shopping","name":"買い物","tasks":[{"title":"牛乳を買う","listId":"shopping"}]}
In Node.js, the same JSON was printed and the exit code was 0. When you need the original list from a task, look it up using listId.
Keep only the loop-forming property out of the JSON
If your screen logic needs to reach the list directly from a task, leave the reference in place and remove that property only when calling JSON.stringify.
The first way is to pass a function (a replacer) as the second argument to JSON.stringify. As described in the replacer section of MDN's JSON.stringify() page, the function receives each property's key and value, and any property for which it returns undefined is left out of the output.
const json = JSON.stringify(list, (key, value) => (key === 'list' ? undefined : value));
No error occurred, and the screen showed 保存しました:{"name":"買い物","tasks":[{"title":"牛乳を買う"}]}.
The second way is to write a toJSON() method on the class. As described in MDN's toJSON() explanation, when an object has toJSON(), its return value is used as the JSON.
class Task {
constructor(title, list) {
this.title = title;
this.list = list;
}
toJSON() {
return { title: this.title };
}
}
I kept the TaskList class from before and tried this. No error occurred, and the same {"name":"買い物","tasks":[{"title":"牛乳を買う"}]} as with the replacer was saved. Because list is dropped every time a Task is passed to JSON.stringify, callers don't need to write a replacer each time.
Referencing the same object from two places does not cause an error if there is no loop
A case that is easy to confuse with a circular reference is when the same object is referenced from two places. I made two tasks each hold the same urgent object as a tag.
const urgent = { label: '急ぎ' };
const list = {
name: '買い物',
tasks: [
{ title: '牛乳を買う', tag: urgent },
{ title: 'パンを買う', tag: urgent },
],
};
This does not cause an error, and urgent was written out in each place.
{"name":"買い物","tasks":[{"title":"牛乳を買う","tag":{"label":"急ぎ"}},{"title":"パンを買う","tag":{"label":"急ぎ"}}]}
With this data, there is no path from urgent back to list, so there is no loop and no error occurred.
Replacing the cycle with a placeholder without knowing where it is
Even when you can't predict which property forms the loop, you can handle it with a general-purpose replacer. MDN's "TypeError: cyclic object value" page includes an example of a replacer that finds circular references and replaces them with the string "[Circular]". It keeps an array of the objects currently on the path being traversed (the parent, its parent, and so on) and replaces only values that point back to one of them.
I used getCircularReplacer from that example as is, with data where two tasks point to the list and both share the same urgent tag.
const urgent = { label: '急ぎ' };
const list = { name: '買い物', tasks: [] };
list.tasks.push({ title: '牛乳を買う', tag: urgent, list });
list.tasks.push({ title: 'パンを買う', tag: urgent, list });
const json = JSON.stringify(list, getCircularReplacer());
No error occurred. Only list became "[Circular]", and the tag on the second task was kept.
{"name":"買い物","tasks":[{"title":"牛乳を買う","tag":{"label":"急ぎ"},"list":"[Circular]"},{"title":"パンを買う","tag":{"label":"急ぎ"},"list":"[Circular]"}]}
A replacer that stores every object it has seen in a WeakSet and returns undefined on the second encounter also drops references that are not circular. With the same data, no error occurred, but the tag of the second task was removed from the JSON, leaving only {"title":"パンを買う"}. Nothing is printed to the console, so the missing data is easy to overlook.
A general-purpose replacer only suppresses the error; the looping part does not remain in the JSON. MDN's example also notes that with this approach, part of the data is lost. If you plan to read the saved data back and use it, either give the child an ID, or decide which properties to exclude and remove them with a replacer or toJSON(). That way, the code makes clear what goes into the JSON.




