Ask Your Question

What is the quickest method to verify if a string is JSON using PHP?

asked 2021-12-22 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2023-04-01 15:00:00 +0000

bukephalos gravatar image

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 {
    return json_last_error() === JSON_ERROR_NONE;

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

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2021-12-22 11:00:00 +0000

Seen: 18 times

Last updated: Apr 01 '23