Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

json_validate()

Check JSON syntax without materializing a PHP value, available since PHP 8.3.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Check JSON syntax without materializing a PHP value, available since PHP 8.3. A top-level scalar such as null is valid, whereas a trailing comma is invalid. Depth limits nesting. JSON_INVALID_UTF8_IGNORE is the only optional flag supported in PHP 8.4. If the decoded value is needed, decoding once avoids parsing twice.

Signature

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

Example output

[true,false,false,true]

Examples

<?php
echo json_encode([json_validate("null"),json_validate("[1,]"),json_validate("[[1]]",2),json_validate("[[1]]",3)]);

Result

[true,false,false,true]

Sources