Open In App

How to Validate JSON in PHP ?

Last Updated : 11 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to validate JSON strings in PHP. 

JSON is an abbreviation for JavaScript Object Notation. It is an extensively used data interchange format for online services that are easily understood by humans. To determine if the JSON output is genuine, PHP includes a method called json_decode(), which was introduced in PHP 5.3. 

It is a PHP built-in function that is used to decode a JSON string. It creates a PHP variable from a JSON encoded text.

Syntax: 

json_decode( $json, $assoc = FALSE, $depth = 512, $options = 0 )

Parameters: As previously stated, this function requires four parameters:

  • json: It is required. It contains the JSON string that must be decoded.
  • assoc: It is optional. A boolean value is specified. When this property is set to “true”, the returned object is transformed into an associative array.
  • depth: It is optional. The depth of recursion is specified.
  • options: It is optional which includes bitmask specification as (JSON_OBJECT_AS_ARRAY,JSON_BIGINT_AS_STRING,JSON_THROW_ON_ERROR).

Return values: The encoded JSON value is returned in the proper PHP type by this method. It returns NULL if the JSON cannot be decoded if the encoded data is deeper than the recursion limit.

Example 1: The following code demonstrates the validation of JSON in PHP by using the following methods.

  • This condition checks whether the $data variable is empty. If $data is null, it will return false.
if (!empty($data))
  • The is_string() method determines if a variable is of the string type or not.
is_string($data)
  • json_decode($data, true) will return an object converted into an associative array, and then the is_array() function checks whether the object is an array or not.
is_array(json_decode($data, true))

PHP




<?php
    function json_validator($data) {
        if (!empty($data)) {
            return is_string($data) && 
              is_array(json_decode($data, true)) ? true : false;
        }
        return false;
    }
  
    $data1 = '{"name":"vishal", "email": "abc@gmail.com"}';
    $data2 = '{name:vishal, email: abc@gmail.com}';
  
    echo "Result for data1: ";
    echo (json_validator($data1) ? "JSON is Valid\n" 
        : "JSON is Not Valid\n");
    echo "Result for data2: ";
    echo (json_validator($data2) ? "JSON is Valid" 
        : "JSON is Not Valid");
?>


Output:

 

Example 2: If the string is legitimate, json_decode will process it and return the PHP variable. If the string is invalid, the error will be generated. The error will be suppressed by the character “@”.

@json_decode($data)

This will determine whether $data is a valid JSON string by comparing it to JSON_ERROR_NONE. The json_last_error() returns the last error if there were any errors when json_decode() was invoked.

return (json_last_error() === JSON_ERROR_NONE)

PHP




<?php
    function json_validator($data) {
        if (!empty($data)) {
            @json_decode($data);
            return (json_last_error() === JSON_ERROR_NONE);
        }
        return false;
    }
  
    $data1 = '{"name":"vishal", "email": "abc@gmail.com"}';
    $data2 = '{name:vishal, email: abc@gmail.com}';
  
    echo "Result for data1: ";
    echo (json_validator($data1) ? "JSON is Valid\n" 
        : "JSON is Not Valid\n");
    echo "Result for data2: ";
    echo (json_validator($data2) ? "JSON is Valid" 
        : "JSON is Not Valid");
?>


Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads