PHP json_validate() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
JSON

Definition

In PHP, json_validate() checks whether a string contains valid JSON. It returns true or false and does not build any PHP value. It is available in PHP 8.3 and later.

Before PHP 8.3 you had to call json_decode() just to find out if text was valid JSON, which wastes memory on big strings. json_validate() only checks the syntax, so it is faster and lighter when you do not need the decoded data.

Syntax

Call json_validate() with the values you need. Start with this line.

PHP
$isValid = json_validate($json);
CallWhat it does
json_validate('{"a":1}')Returns true because the JSON is valid.
json_validate('{a:1}')Returns false because the key is not in double quotes.
json_validate($json, 5)Returns true only if the JSON is valid and nested no deeper than 5 levels.

json_validate() requires PHP 8.3 or newer. It also updates json_last_error() and json_last_error_msg(), so you can find out why validation failed. The only flag it accepts is JSON_INVALID_UTF8_IGNORE.

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

Parameters

ParameterDescription
jsonRequired. The string to check. An empty string is not valid JSON.
depthOptional. The maximum nesting depth allowed. The default is 512. It must be greater than 0, otherwise PHP throws a ValueError.
flagsOptional. The only supported flag is JSON_INVALID_UTF8_IGNORE. Any other flag throws a ValueError. The default is 0.

Return Value

Return ValueDescription
booltrue if the string is valid JSON within the depth limit, otherwise false.

PHP Examples

Example 1 — Validate JSON Strings

Check a few strings and see which ones are valid JSON.

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

echo json_validate('[1,2,3]') ? 'Valid' : 'Invalid', PHP_EOL;

How It Works

  1. The first string uses double quotes around the key, so it is valid JSON.
  2. The second string has no quotes around name, so it is not valid JSON.
  3. json_validate() just returns a bool and does not decode anything.
  4. The array of numbers is valid, so the ternary prints Valid.

Example 2 — Depth Limits and Empty Strings

Check how the depth argument changes the result, and what happens with an empty string.

PHP
<?php
var_dump(json_validate('[[1]]', 1));
echo json_last_error_msg(), PHP_EOL;

var_dump(json_validate('[[1]]', 2));

var_dump(json_validate(''));
echo json_last_error_msg(), PHP_EOL;

How It Works

  1. The JSON has two levels of nesting, but the first call only allows one.
  2. json_validate() returns false and json_last_error_msg() explains the depth problem.
  3. With a depth of 2 the same string is valid.
  4. An empty string is never valid JSON, so it fails with a syntax error.

Example 3 — A Version-Safe Validator

Use json_validate() when it exists, and fall back to json_decode() on older PHP versions.

PHP
<?php
function isValidJson(string $json): bool
{
    if (function_exists('json_validate')) {
        return json_validate($json);
    }

    json_decode($json);
    return json_last_error() === JSON_ERROR_NONE;
}

echo isValidJson('{"id":7}') ? 'Valid' : 'Not valid', PHP_EOL;
echo isValidJson('{"id":') ? 'Valid' : 'Not valid', PHP_EOL;
echo isValidJson('"just a string"') ? 'Valid' : 'Not valid', PHP_EOL;

How It Works

  1. function_exists() checks whether json_validate() is available on this PHP version.
  2. On PHP 8. 3 and newer the helper uses json_validate() directly.
  3. On older versions it decodes the text and checks json_last_error() instead.
  4. A plain JSON string in double quotes is valid JSON, so the last call prints Valid.

Frequently Asked Questions

It checks if a string is valid JSON and returns true or false. Unlike json_decode(), it does not create a PHP value, so it uses less memory.
It was added in PHP 8.3. On PHP 8.2 or older, calling it causes a fatal error, so use function_exists() or json_decode() with json_last_error() as a fallback.
json_validate() only answers yes or no. json_decode() actually builds the data. If you need the data anyway, just use json_decode() with JSON_THROW_ON_ERROR.
Yes. It updates the global JSON error state, so json_last_error() and json_last_error_msg() tell you the reason, such as a syntax error or a depth problem.
No. An empty string is not valid JSON, so json_validate('') returns false. A string that holds just a number, a quoted text, true, false or null is valid.

Back to JSON Functions

Browse all PHP JSON function tutorials.

JSON functions →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful