Reputation: 8155
Does anyone know of a robust (and bullet proof) is_JSON function snippet for PHP? I (obviously) have a situation where I need to know if a string is JSON or not.
Hmm, perhaps run it through a JSONLint request/response, but that seems a bit overkill.
Upvotes: 48
Views: 93696
Reputation: 7606
Finally! As of PHP 8.3 one can use the new json_validate() function:
$fixtures = [
// Not valid
'foo',
'{foo: bar}',
"'{'foo': 'bar'}",
'{...}',
// Valid
'"foo"',
'1',
'{"foo": "bar"}',
];
foreach ($fixtures as $string) {
if (json_validate($string)) {
echo sprintf('YES, >%s< is a valid JSON string', $string).PHP_EOL;
echo 'decoded: '. var_export(json_decode($string), true).PHP_EOL;
} else {
echo sprintf('NO, >%s< is NOT a valid JSON string: %s', $string, json_last_error_msg()). PHP_EOL;
}
}
This function only accepts a string as the first argument: output of this snippet
Upvotes: 0
Reputation: 81
This is the best and efficient way
function isJson($string) {
return (json_decode($string) == null) ? false : true;
}
Upvotes: 4
Reputation: 44215
If you are using the built in json_decode
PHP function, json_last_error
returns the last error (e.g. JSON_ERROR_SYNTAX
when your string wasn't JSON).
Usually json_decode
returns null
anyway.
Upvotes: 70
Reputation: 3867
For my projects I use this function (please read the "Note" on the json_decode() docs).
Passing the same arguments you would pass to json_decode() you can detect specific application "errors" (e.g. depth errors)
With PHP >= 5.6
// PHP >= 5.6
function is_JSON(...$args) {
json_decode(...$args);
return (json_last_error()===JSON_ERROR_NONE);
}
With PHP >= 5.3
// PHP >= 5.3
function is_JSON() {
call_user_func_array('json_decode',func_get_args());
return (json_last_error()===JSON_ERROR_NONE);
}
Usage example:
$mystring = '{"param":"value"}';
if (is_JSON($mystring)) {
echo "Valid JSON string";
} else {
$error = json_last_error_msg();
echo "Not valid JSON string ($error)";
}
Upvotes: 21
Reputation: 1678
Doesn't json_decode()
with a json_last_error()
work for you? Are you looking for just a method to say "does this look like JSON" or actually validate it? json_decode()
would be the only way to effectively validate it within PHP.
Upvotes: 4
Reputation: 191
$this->post_data = json_decode( stripslashes( $post_data ) ); if( $this->post_data === NULL ) { die( '{"status":false,"msg":"The post_data parameter must be valid JSON"}' ); }
Upvotes: 2
Reputation: 401182
What about using json_decode
, which should return null
if the given string was not valid JSON-encoded data ?
See example 3 on the manual page :
// the following strings are valid JavaScript but not valid JSON
// the name and value must be enclosed in double quotes
// single quotes are not valid
$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // null
// the name must be enclosed in double quotes
$bad_json = '{ bar: "baz" }';
json_decode($bad_json); // null
// trailing commas are not allowed
$bad_json = '{ bar: "baz", }';
json_decode($bad_json); // null
Upvotes: 17