CKTIS / PHP
JsonException
Catch failures from JSON operations using JSON_THROW_ON_ERROR.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
catch (JsonException $error) { /* inspect JSON error */ }Catch failures from JSON operations using JSON_THROW_ON_ERROR. Its code identifies the JSON error, such as JSON_ERROR_SYNTAX. A valid JSON null decodes normally, allowing the caller to distinguish absence from malformed input. The ordinary default flags do not enable this exception mode.
try { json_decode("{",true,512,JSON_THROW_ON_ERROR); }
catch(JsonException $e){$syntax=$e->getCode()===JSON_ERROR_SYNTAX;}
echo json_encode([$syntax,json_decode("null",true,512,JSON_THROW_ON_ERROR)]);
[true,null]
Examples
<?php
try { json_decode("{",true,512,JSON_THROW_ON_ERROR); }
catch(JsonException $e){$syntax=$e->getCode()===JSON_ERROR_SYNTAX;}
echo json_encode([$syntax,json_decode("null",true,512,JSON_THROW_ON_ERROR)]);
Result
[true,null]