PHP error guide
JSON maximum stack depth exceeded: meaning and fix
Error summary
Reject unreasonable nesting or set a measured depth within the supported range. JSON nesting exceeds the depth argument used by the encoder or decoder.
What it means
Reject unreasonable nesting or set a measured depth within the supported range. JSON nesting exceeds the depth argument used by the encoder or decoder.
What the error means
This message means that JSON nesting exceeds the depth argument used by the encoder or decoder. The exact signature distinguishes json maximum stack depth exceeded from a generic application failure.
Why PHP produces it
The engine or service reports “JSON maximum stack depth exceeded” because its required precondition was not met. Reject unreasonable nesting or set a measured depth within the supported range.
PHP version notes
The json maximum stack depth exceeded 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: JSON nesting exceeds the depth argument used by the encoder or decoder
- The code path assumes the prerequisite for json maximum stack depth exceeded has already been satisfied.
- For json maximum stack depth exceeded, 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 maximum stack depth exceeded.
Minimal examples
BAD — reproduces the problem
json_decode($deep, true, 2, JSON_THROW_ON_ERROR);
FIXED — safer pattern
json_decode($payload, true, 64, JSON_THROW_ON_ERROR);
Step-by-step diagnosis
- Copy the complete “JsonException: Maximum stack depth exceeded” text and retain the first application stack frame.
- Reproduce json maximum stack depth exceeded in the same SAPI and environment listed for this page.
- Before changing json maximum stack depth exceeded, inspect the preceding value or directive and verify its type, path, version, and permissions.
- Apply the narrow correction—reject unreasonable nesting or set a measured depth within the supported range—then repeat the original request once.
Fixes
Correct the failing prerequisite
Reject unreasonable nesting or set a measured depth within the supported range
json_decode($payload, true, 64, JSON_THROW_ON_ERROR);
Fail explicitly at the boundary
Validate the condition before the operation that emits json maximum stack depth exceeded, and log a safe diagnostic without credentials or full production paths.
Common mistakes when fixing it
- Suppressing json maximum stack depth exceeded instead of correcting its upstream condition.
- Testing json maximum stack depth exceeded only with the CLI binary when the failing request runs under FPM or Apache.
- Changing a global setting for json maximum stack depth exceeded before confirming the site-specific effective configuration.
How to prevent it
- Add a focused test that exercises the boundary responsible for json maximum stack depth exceeded.
- Keep runtime versions, extensions, configuration, and deploy artifacts affecting json maximum stack depth exceeded reproducible.
- Validate external data and service return values before they can trigger json maximum stack depth exceeded.
Web server / environment notes
cli, fpm, linux. The failure occurs where jSON nesting exceeds the depth argument used by the encoder or decoder
Tags: cli,fpm,linux