PHP.nl

json_validate

json_validate

Checks if a string contains valid JSON

bool **json_validate** string $json int $depth int $flags

Returns whether the given string is syntactically valid JSON. If returns true, will successfully decode the given string when using the same and . json_validate``json_decode``depth``flags

If returns false, the cause can be retrieved using and . json_validate``json_last_error``json_last_error_msg

uses less memory than
if the decoded JSON payload is

not used, because it does not need to build the array or object structure containing the payload. json_validate``json_decode

Let op: > Calling immediately before will unnecessarily parse the string twice, as implicitly performs validation during decoding. json_validate``json_decode``json_decode

 should therefore only be used
if the decode JSON payload is not immediately used and knowing whether
the string contains valid JSON is needed.

json_validate

jsonThe string to validate.

This function only works with UTF-8 encoded strings.

depth Maximum nesting depth of the structure being decoded. The value must be greater than , and less than or equal to . 0``2147483647

flags Currently only

   is accepted.
  `JSON_INVALID_UTF8_IGNORE`

Returns true if the given string is syntactically valid JSON, otherwise returns false.

If is outside the allowed range, a is thrown. depth``ValueError

If is not a valid flag, a is thrown. flags``ValueError

Voorbeeld: examples

<?php
var_dump(json_validate('{ "test": { "foo": "bar" } }'));
var_dump(json_validate('{ "": "": "" } }'));
?>
bool(true)
bool(false)

json_decode``json_last_error``json_last_error_msg