Member-only story
json_validate()
—the lean way to sanity-check JSON in PHP 8.3+
A lightweight guardrail for PHP 8.3-plus that validates JSON without the overhead of decoding.
When PHP 8.3 landed on 23 November 2023 it quietly shipped a new standard-library helper: json_validate()
.
At first glance the function looks almost trivial—it just returns true
or false
—but it fills a long-standing gap in the language.
🔓 Not a Medium member yet? Click here to access this article for FREE!
Why we needed something new
Until 8.3 every production code-base followed one of three patterns:
1. Decode-and-check
$data = json_decode($raw);
if (json_last_error() !== JSON_ERROR_NONE) {
/* handle error */
}
2. This works, but json_decode()
builds PHP arrays/objects even if you throw the result away. That costs memory and CPU for large payloads .
3. Exception mode (PHP 7.3+)
try {
$data = json_decode($raw, flags: JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
// handle
}
4. Clearer control flow, but still forces a full decode.