How to Validate JSON String in PHP

On 12/11/2017

Hi, we'll see how to validate json string in this PHP tutorial. JSON is a light-weight data exchange format among web services and it is very human readable. At times it is required to check if the json output you received is a valid one. PHP programming language does not provide any direct method to validate a json string but sure there is some workaround to determine if a string is a valid json format or not. You need PHP version 5.3 or above for this to work out.

php-validate-json-string-format

There are 4 built-in functions provided by PHP to deal with json format namely json_encode(), json_decode(), json_last_error_msg() and json_last_error().

The methods json_encode() and json_decode() are used to encode and decode json format and the working example of them are already covered in this blog.

The other two methods json_last_error_msg() and json_last_error() should be used in conjunction with the json encode or decode methods.

How to Validate JSON String in PHP?

The php function json_last_error() takes no parameters and returns the error (in case) encountered during the last json encoding/decoding process. If there is no error then it returns an integer constant "JSON_ERROR_NONE". Check here for the complete list of error constants returned by this method.

So by decoding the given string and checking it for errors, we can determine if it's a valid json string or not.

Here is the php code to validate json string.

PHP Function to Validate JSON

<?php
function validate_json($str=NULL) {
    if (is_string($str)) {
        @json_decode($str);
        return (json_last_error() === JSON_ERROR_NONE);
    }
    return false;
}
?>

Function Usage

You can use the above function validate_json() like below to determine a valid json string.

<?php
    echo (validate_json('{"format": "json"}') ? "Valid JSON" : "Invalid JSON");
    // prints 'Valid JSON'
    echo (validate_json('{format: json}') ? "Valid JSON" : "Invalid JSON");
    // prints 'Invalid JSON'
    echo (validate_json(array()) ? "Valid JSON" : "Invalid JSON");
    // prints 'Invalid JSON'
    echo (validate_json() ? "Valid JSON" : "Invalid JSON");
    // prints 'Invalid JSON'
?>
Read:

Thus with a small workaround you can validate a json string easily in php.

15 comments:

  1. There Are Lots Of validator and reformatter for JSON Code and Tutorials reformatter for JSON

    ReplyDelete
  2. Great post. I appreciate your work. Thanks for sharing.

    html training in chennai

    ReplyDelete
  3. This technical post helps me to improve my skills set, thanks for this wonder article I expect your upcoming blog, so keep sharing..
    Regards,
    the best dot net training institute

    ReplyDelete
  4. Nice piece of information on HTML5. With the expansion of smartphones and other portable gadgets, the demand for responsive website design that go comfy on all devices keeps on increasing. This leads to invention and expansion of HTM5 web technology. the best dot net training institute

    ReplyDelete
  5. Great post.100% Job Oriented R Programming Training In Chennai for more Information click to the best cloud-computing training in chennai

    ReplyDelete

  6. This is an awesome post. Really very informative and creative content. Thanks for sharing these concepts. I like this site very much and it helped me to improve my skills. Thank you for this brief explanation and very nice information.
    Thanks,
    Placement Training in chennai | Placement courses in Chennai | Placement Training institutes in Chennai

    ReplyDelete
  7. I have read your blog andI got very useful and knowledgeable information from your blog. It’s really a very nice article. You have done a great job
    Good presentation thanks for the helpful blog.
    Regards,
    DevOps Consult

    ReplyDelete
  8. Thank you for letting us to do Validate JSON String in PHP it helps in my big data training

    ReplyDelete
  9. That is very interesting; you are a very skilled blogger. I have shared your website in my social networks! A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article.

    big data training in chennai|

    ReplyDelete

Contact Form

Name

Email *

Message *