What this thread does
Do first: decode at the request boundary with json_decode($json, true, 512, JSON_THROW_ON_ERROR) and handle JsonException separately from a successful null result.
Short answer
As of November 2026, PHP 8.5.12 is dated October 22, 2026 in the changelog. The decode changelog dates JSON_THROW_ON_ERROR support to PHP 7.3.0. The JSON text null returns PHP null without a JsonException: a null return is not an error test. Apply any API rule prohibiting null after decoding succeeds (constants, Andrea Faulds' September 10, 2017 RFC).
Choose / don't choose
- Exception mode: choose on a supported PHP runtime to leave the global error state untouched; don't branch on
$value === nullto reject JSON syntax (RFC). - Legacy error mode: on runtimes predating the flag, call
json_last_error()immediately after decoding and compare withJSON_ERROR_NONE; usejson_last_error_msg()for a diagnostic. Don't use an unrelated later call's error state (error API, message API). - Strict UTF-8 input: choose rejection for invalid bytes unless the API explicitly permits lossy recovery; don't silently discard input bytes merely to avoid an exception (constants).
Not for: PHP runtime security patches; see php-rce-october-security-patch-runtime-image-audit.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
json_decode / flags | Use JSON_THROW_ON_ERROR; default flags are 0 | PHP 7.3.0+ | decode, constants |
depth | Default 512; allowed 1..2147483647; set the decoder budget to the API's documented nesting allowance and check boundary payloads | Current PHP docs; invalid argument raises ValueError since PHP 8.0.0 | decode |
associative | true returns objects as arrays; false returns objects; default null follows JSON_OBJECT_AS_ARRAY | Current PHP docs | decode |
JSON_INVALID_UTF8_IGNORE / JSON_INVALID_UTF8_SUBSTITUTE | Leave unset for strict input; JSON_INVALID_UTF8_IGNORE ignores invalid UTF-8 characters; JSON_INVALID_UTF8_SUBSTITUTE replaces invalid UTF-8 characters | Current PHP JSON options | constants |
The contract has no stated nesting number here, so 512 in the example is the library default, not an API-specific recommendation. A legal depth exceeded by the payload is a JSON decoding failure; a depth argument outside the allowed range is a programming/configuration error.
Order of operations
1. Establish a valid depth budget and the API's UTF-8 policy before handling the request. 2. Decode inside a narrow exception boundary:
try {
$value = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $error) {
// Reject this request as malformed JSON; stop this handler.
return;
}
// null here is a successful JSON value; apply schema rules next.
3. Validate the decoded value's schema/type after successful decoding. Do not convert a caught JsonException into a null sentinel; that recreates the ambiguity. The JsonException API exposes its error code/message; the exception flag does not update the old global JSON error state (constants).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
Syntax error | Malformed JSON | Catch JsonException and reject input; do not treat the result as valid null | dtar's example, flag |
ValueError | The depth argument is outside its valid numeric range | Correct the configured depth argument | decode exceptions |
Missing
- No API nesting contract or schema is specified; neither a universal depth number nor permission to accept a top-level null follows from valid JSON syntax.
Checked
2026-11-12; PHP 8.5.12 changelog, JSON decode/constants/error/exception references, Andrea Faulds' RFC and dtar's dated example read. No PHP code or request validation executed.
Started from a post by @PHP Documentation Group, packaged by the editor. Original post