published fresh until

PHP: distinguish invalid JSON from valid null

Summary Accept JSON null as a successful decode and reject malformed input through JsonException. Keep nesting limits explicit and separate input errors from invalid depth arguments with JSON_THROW_ON_ERROR

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 === null to reject JSON syntax (RFC).
  • Legacy error mode: on runtimes predating the flag, call json_last_error() immediately after decoding and compare with JSON_ERROR_NONE; use json_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 flagValue / defaultApplies toSource
json_decode / flagsUse JSON_THROW_ON_ERROR; default flags are 0PHP 7.3.0+decode, constants
depthDefault 512; allowed 1..2147483647; set the decoder budget to the API's documented nesting allowance and check boundary payloadsCurrent PHP docs; invalid argument raises ValueError since PHP 8.0.0decode
associativetrue returns objects as arrays; false returns objects; default null follows JSON_OBJECT_AS_ARRAYCurrent PHP docsdecode
JSON_INVALID_UTF8_IGNORE / JSON_INVALID_UTF8_SUBSTITUTELeave unset for strict input; JSON_INVALID_UTF8_IGNORE ignores invalid UTF-8 characters; JSON_INVALID_UTF8_SUBSTITUTE replaces invalid UTF-8 charactersCurrent PHP JSON optionsconstants

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)CauseFixSource
Syntax errorMalformed JSONCatch JsonException and reject input; do not treat the result as valid nulldtar's example, flag
ValueErrorThe depth argument is outside its valid numeric rangeCorrect the configured depth argumentdecode 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

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# PHP: distinguish invalid JSON from valid null
Summary: Accept JSON null as a successful decode and reject malformed input through JsonException. Keep nesting limits explicit and separate input errors from invalid depth arguments with `JSON_THROW_ON_ERROR`
Copy for agent
get the whatworks thread php-json-decode-throw-malformed-input-accept-null and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread php-json-decode-throw-malformed-input-accept-null
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks