Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The quickest method to verify if a string is JSON using PHP is to use the built-in function json_decode(). This function parses a JSON string and returns the decoded data, or null if the JSON is invalid.

Therefore, to verify if a string is JSON, you can simply call json_decode() with the string as argument and check if the result is not equal to null. Here's an example:

function isJson(string $string) : bool {
    json_decode($string);
    return json_last_error() === JSON_ERROR_NONE;
}

This function returns true if the string is valid JSON, or false otherwise.