json_validate()
json_validate() — Checks if a string contains valid JSON.
Built-in function of PHP 8.5. The manual lists it as available in PHP 8 >= 8.3.0. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Returns whether the given string is syntactically valid JSON. If json_validate() returns true, json_decode() will successfully decode the given string when using the same depth and flags.
Signature
json_validate(string $json, int $depth = 512, int $flags = 0): bool
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $json | string | The string to validate. |
| $depth | int | Maximum nesting depth of the structure being decoded. The value must be greater than 0, and less than or equal to 2147483647. |
| $flags | int | Currently only JSON_INVALID_UTF8_IGNORE is accepted. |
Return value
Returns true if the given string is syntactically valid JSON, otherwise returns false.
Example
A runnable snippet:
var_dump(json_validate('{ "test": { "foo": "bar" } }')); var_dump(json_validate('{ "": "": "" } }'));
Output:
bool(true) bool(false)
See also
json_decode(), json_last_error(), json_last_error_msg()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page json_validate() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.