Since PHP 7.3 one can use the JSON_THROW_ON_ERROR
constant.
try {
$jsonObj = json_decode($jsonData, true, $depth=512, JSON_THROW_ON_ERROR);
} catch (Exception $e) {
// handle exception
}
More: https://www.php.net/manual/de/function.json-decode.php#refsect1-function.json-decode-changelog