How to validate JSON in PHP older than 5.3.0?

user955822 picture user955822 · Oct 20, 2011 · Viewed 60.1k times · Source

Is there any way to check that a variable is a valid JSON string in PHP without using json_last_error()? My PHP version is older than 5.3.0.

Answer

Jeff Lambert picture Jeff Lambert · Oct 20, 2011
$ob = json_decode($json);
if($ob === null) {
 // $ob is null because the json cannot be decoded
}