Json Function And Php

List of Json function in Php.

  • json_decode:Decodes a Json string.
  • json_encode:Returns the Json representation of a value.
  • json_last_error_msg: Returns the error string of the last json_encode() or json_decode() call.
  • json_last_error : Returns the last error occurred.

json_decode

json_decode converts Json encoded string into a Php variable.

Syntax

mixed json_decode ($json [,$assoc=false [, $depth= 512[,$options= 0 ]]])

Parameters

  • json_string :The string being decoded.json_decode function only works with UTF-8 encoded strings.
  • assoc :When set to TRUE, returned objects will be converted into associative arrays.
  • depth:It specified recursion depth.
  • options:Bitmask of Json decode options.

Example

Below Example shows how to decode Json objects in Php.

<?php 
$json = '{"First Name":"Manish","Last Name":"Mishra"}';
var_dump(json_decode($json));
?>  

The above example will output:

object(stdClass)#1 (2) 
{
["First Name"]=> string(6) "Manish" 
["Last Name"]=> string(6) "Mishra"
} 

json_encode

json_encode function is used for encoding Json in Php.

Syntax

string json_encode (mixed $value[,int $options = 0 [,int $depth = 512]])

Parameters

  • value :The value being encoded.json_encode function only works with UTF-8 encoded data.
  • options :Bitmask consisting of JSON_HEX_TAG, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_UNESCAPED_SLASHES, JSON_PRETTY_PRINT, JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE,JSON_HEX_QUOT,JSON_HEX_AMP.
  • depth :Must be greater than zero.

Example

Below Example shows how to encode arrays into Json in Php.

<?php 
$json = array('First Name' => 'Manish', 'Last Name' => 'Mishra');
var_dump(json_encode($json));
?> 

The above example will output:

string(44) 
"{
"First Name":"Manish",
"Last Name":"Mishra"
}"

json_last_error_msg

json_last_error_msg function returns the error string (if any) occurred during the last json_encode() or json_decode() call.

Syntax

string json_last_error_msg (void)

Parameters

  • json_last_error_msg function has no pramerter.

json_last_error

json_last_error function returns the last error (if any) occurred during the last json_encode() or json_decode() call.

Syntax

int json_last_error ( void )

Parameters

  • json_last_error function has no pramerter.

Return Values

json_last_error function returns an integer corresponding to the following constants:

Constant Value
JSON_ERROR_NONE 0
JSON_ERROR_DEPTH 1
JSON_ERROR_STATE_MISMATCH 2
JSON_ERROR_CTRL_CHAR 3
JSON_ERROR_SYNTAX 4
JSON_ERROR_UTF8 5

Example

Below Example shows how json_last_error function works.

<?php 
// A valid json string
$json[] = '{"First Name": "Manish"}';

// An invalid json string which will cause an syntax 
// error, in this case we used ' instead of " for quotation
$json[] = "{'First Name': 'Manish'}";
foreach ($json as $string) {
    echo 'Decoding: ' . $string;
    json_decode($string);
    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            echo ' - No errors';
        break;
        case JSON_ERROR_STATE_MISMATCH:
            echo ' - Underflow or the modes mismatch';
        break;
        case JSON_ERROR_DEPTH:
            echo ' - Maximum stack depth exceeded';
        break;
        case JSON_ERROR_CTRL_CHAR:
            echo ' - Unexpected control character found';
        break;
        case JSON_ERROR_SYNTAX:
            echo ' - Syntax error, malformed JSON';
        break;
        default:
            echo ' - Unknown error';
        break;
    }
    echo PHP_EOL;
}
?>  

The above example will output:

Decoding: {"First Name": "Manish"} - No errors
Decoding: {'First Name': 'Manish'} - Syntax error, malformed JSON