Skip to content

Core\Json::isValid

Checks whether a text is valid JSON and returns true or false.

isValid returns true when the text is a valid JSON document at the default depth of 512. It returns false for text that is not JSON and for a document nested deeper than 512 levels. It also returns false for a whole number too large or too small for int. It never throws an error, so you do not need a try block.

isValid reads the text and builds no values. It is faster than decode and uses less memory. Each request and each script has a memory limit. A valid document can be too large for decode to read within that limit. isValid still returns true for it.

isValid does not return the value. When you need the value too, call only Core\Json::decode. It throws a ParseError for text that is not valid JSON. This way the text is read once.

The examples below check a few short texts and check the lines of a log file one by one. The last one rejects a request body that is not JSON before the program does anything else with it.

Parameters

  • $jsonstringThe JSON text to check.

Return value

Returns bool. true when $json is a valid JSON document. false for text that is not valid JSON, nesting deeper than 512, or a whole number too large or too small for int. A valid document returns true even when decode would run out of memory for it.

Changelog

VersionChange
0.0.5Core\Json::isValid checks a document without building its values, so it is faster and uses less memory. It returns true for a valid document that is too large to decode under the memory limit.

Examples

Checking a few texts

<?nvs
// Shows which texts `Core\Json::isValid` accepts as JSON.
// JSON needs double quotes around keys and strings, and allows no comma after the last item.
array<string> $texts = [
"{\"name\": \"Ada\"}",
'[1, 2, 3]',
'true',
"{name: \"Ada\"}",
'[1, 2, 3,]',
"'hello'",
];
// This prints `yes` for the first three texts and `no` for the last three.
foreach ($texts as string $text) {
echo $text, ' -> ', Core\Json::isValid($text) ? 'yes' : 'no', "\n";
}
Output
{"name": "Ada"} -> yes
[1, 2, 3] -> yes
true -> yes
{name: "Ada"} -> no
[1, 2, 3,] -> no
'hello' -> no

Counting the broken lines of a log

<?nvs
// Shows how to find the broken lines in a log where each line is one JSON object.
// The third line was cut off when the program that wrote it stopped.
string $log = "{\"level\":\"info\",\"msg\":\"started\"}\n"
. "{\"level\":\"warn\",\"msg\":\"disk almost full\"}\n"
. "{\"level\":\"error\",\"ms\n"
. "{\"level\":\"info\",\"msg\":\"stopped\"}";
// `Core\Str::split` gives one string for each line.
// This prints the number of each broken line, then the count: line 3, and 1 of 4.
uint $number = 0;
uint $broken = 0;
foreach (Core\Str::split($log, "\n") as string $line) {
$number = $number + 1;
if (!Core\Json::isValid($line)) {
echo 'line ', $number, " is not valid JSON\n";
$broken = $broken + 1;
}
}
echo $broken, ' of ', $number, " lines are broken\n";
Output
line 3 is not valid JSON
1 of 4 lines are broken

Rejecting a request body

<?nvs
// Shows how an API checks a request body before it does any other work.
class Api
{
// Returns the status code and the message the API sends back for one body.
public static function answer(string $body): string
{
if (!Core\Json::isValid($body)) {
return '400 the body must be JSON';
}
return '202 accepted, ' . Core\Str::length($body) . ' characters';
}
}
// The second body is a form, not JSON, so this prints `400` for it.
array<string> $bodies = [
"{\"sku\": \"T-1\", \"count\": 2}",
'sku=T-1&count=2',
];
foreach ($bodies as string $body) {
echo Api::answer($body), "\n";
}
Output
202 accepted, 26 characters
400 the body must be JSON