PHP error guide
JSON recursion detected: meaning and fix
Error summary
Serialize an acyclic view model rather than the recursive domain graph. An array or object graph passed to json_encode contains a cycle.
What it means
Serialize an acyclic view model rather than the recursive domain graph. An array or object graph passed to json_encode contains a cycle.
What the error means
This message means that an array or object graph passed to json_encode contains a cycle. The exact signature distinguishes json recursion detected from a generic application failure.
Why PHP produces it
The engine or service reports “JSON recursion detected” because its required precondition was not met. Serialize an acyclic view model rather than the recursive domain graph.
PHP version notes
The json recursion detected wording here is based on PHP 7.x–8.4; exception forms require JSON_THROW_ON_ERROR where shown; punctuation and exception class names can differ on older branches or vendor builds.
Most common causes
- The immediate input or configuration reaches the specific condition: an array or object graph passed to json_encode contains a cycle
- The code path assumes the prerequisite for json recursion detected has already been satisfied.
- For json recursion detected, development and production differ in version, extension, permissions, paths, or service configuration.
- An earlier operation returned an unchecked value that is consumed by the line reporting json recursion detected.
Minimal examples
BAD — reproduces the problem
$a = []; $a["self"] =& $a;
json_encode($a, JSON_THROW_ON_ERROR);
FIXED — safer pattern
json_encode(["id"=>1], JSON_THROW_ON_ERROR);
Step-by-step diagnosis
- Copy the complete “JsonException: Recursion detected” text and retain the first application stack frame.
- Reproduce json recursion detected in the same SAPI and environment listed for this page.
- Before changing json recursion detected, inspect the preceding value or directive and verify its type, path, version, and permissions.
- Apply the narrow correction—serialize an acyclic view model rather than the recursive domain graph—then repeat the original request once.
Fixes
Correct the failing prerequisite
Serialize an acyclic view model rather than the recursive domain graph
json_encode(["id"=>1], JSON_THROW_ON_ERROR);
Fail explicitly at the boundary
Validate the condition before the operation that emits json recursion detected, and log a safe diagnostic without credentials or full production paths.
Common mistakes when fixing it
- Suppressing json recursion detected instead of correcting its upstream condition.
- Testing json recursion detected only with the CLI binary when the failing request runs under FPM or Apache.
- Changing a global setting for json recursion detected before confirming the site-specific effective configuration.
How to prevent it
- Add a focused test that exercises the boundary responsible for json recursion detected.
- Keep runtime versions, extensions, configuration, and deploy artifacts affecting json recursion detected reproducible.
- Validate external data and service return values before they can trigger json recursion detected.
Web server / environment notes
cli, fpm, linux. The failure occurs where an array or object graph passed to json_encode contains a cycle
Tags: cli,fpm,linux