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