# TypeError: Converting circular structure to JSON

**Code:** `circular JSON` · **Category:** JavaScript · **Seen on:** JavaScript

JSON.stringify met an object that references itself (directly or through children).

## Meaning

DOM nodes, framework objects (Express req/res, Mongoose docs), and parent↔child links create cycles that JSON can’t represent.

## Common causes

- Logging/stringifying req/res or DOM elements
- Parent/child references in your data
- ORM documents with back-references

## Quick fix

1. Stringify only the fields you need
2. Use a replacer that skips seen objects
3. Use util.inspect or console.dir for logging

## Detailed fix by platform

### JavaScript

1. 

```javascript
const seen = new WeakSet();
JSON.stringify(obj, (k, v) => (typeof v === 'object' && v !== null ? (seen.has(v) ? undefined : (seen.add(v), v)) : v));
```


## How to diagnose

1. **Object** — Which object (the message names the path)?
2. **Need** — Do you really need the whole object?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/javascript/typeerror-converting-circular-structure-to-json)
