- json_decode: documentation ( source)
- json_last_error: documentation ( source)
<?php
function isJson($string) {
$decoded = json_decode($string);
if ( !is_object($decoded) && !is_array($decoded) ) {
return false;
}
return (json_last_error() == JSON_ERROR_NONE);
}
if ( isJson(123) ) {
echo "valid JSON";
} else {
echo "not valid JSON";
}