I need a function to check whether the incomming response is JSON or NOT in PHP
For eg. my JSON is this
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
I need a function to check whether the incomming response is JSON or NOT in PHP
For eg. my JSON is this
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
You could use json_decode()
on it and then check json_last_error()
. If you have an error, it's not valid JSON.
Keep in mind it's not good enough to just check for null
. The string null
is valid JSON (and it decodes as such).
Yes you can validate Like this.
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
if($json) {
$ob = json_decode($json);
if($ob === null) {
echo 'Invalid Json';
} else {
echo 'Valid Json';
}
}
$json_request = (json_decode($request) != NULL) ? true : false;
Taken from: PHP check whether Incoming Request is JSON type
Does that work?
use this
function check_whether_json($response){
if(json_decode($response) != NULL){
return TRUE;
}else{
return FALSE;
}
}
and CHECK like this
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
Now Checking Starts
if(check_whether_json($json)){
// Proceed ur code...
}