Determining If a String is JSON in PHP


Check if a given string is valid JSON by attempting to decode it.

Source Code

$string = '{"name":"John","age":30}';
$isJson = json_decode($string, true) && (json_last_error() == JSON_ERROR_NONE);
echo $isJson ? "String is valid JSON" : "String is not valid JSON";
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments