Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

json_validate

Checks whether a string is valid JSON.

Syntax

json_validate(string $json, int $depth = 512, int $flags = 0): bool

Parameters

ParameterTypeDescription
$jsonstringThe string to check.
$depthintOptional. The maximum nesting.
$flagsintOptional. Flags.

Return value

bool`true` if the string is valid JSON.

Examples

Valid and invalid JSON:

<?php
 
var_dump(json_validate('{"name":"Anna"}'));
var_dump(json_validate("{'name':'Anna'}"));

Output:

bool(true)
bool(false)

Notes

The function appeared in PHP 8.3. It builds no array, so it is faster than calling json_decode() just to check.

See also

In the tutorial

  • The JSON format — JSON in PHP — json_encode and json_decode, non-ASCII text, error handling and storing data in a file.

Thanks for reading!

Donate to the site — we work hard for you.