# panic: json: infinite recursion (invalid cycle)

- **ID:** `go/encoding-json-marshal-infinite-recursion`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

A struct has a circular reference that causes json.Marshal to recurse infinitely, leading to a stack overflow.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.20 | active | — | — |
| 1.21 | active | — | — |

## Workarounds

1. **Implement json.Marshaler to break cycles or use pointer indirection** (90% success)
   ```
   type Node struct {
    Value int
    Next  *Node
}
func (n *Node) MarshalJSON() ([]byte, error) {
    if n == nil { return json.Marshal(nil) }
    return json.Marshal(struct{ Value int }{Value: n.Value})
}
   ```

## Dead Ends

- **Adding a depth counter to break recursion** — May still panic if depth exceeded; better to use custom marshaler. (50% fail)
