JSON encode decode is one of the most frequently required operations. In this article, we are going to see how to encode and decode JSON using PHP. PHP provides built-in functions to perform these two operations. Those are,
In this article, we are going to learn about these functions with suitable examples. Apart from these functions, a complete guide to handling JSON with PHP will be useful for you to know more about PHP JSON.
Encoding and decoding are the pair of operations which is most importantly used in many application programming. Previously, we have seen PHP functions url_encode() and url_decode() to perform the encoding and decoding of a given URL.
Encoding is used to bundle data with respect to a particular format. This process will be required to preserve data consistency. Decoding is a reverse process that reverts encoded data back to its original form.
In PHP, json_encode() is used to convert PHP-supported data type into JSON formatted string to be returned as a result of JSON encode operation. This function accepts the following set of arguments.
For PHP JSON encodes, the following list of constants will be used for the options parameter of the json_encode() function.
Now, it’s time to see an example PHP program to perform JSON encoding. So, the following program handles a few json_encode() functions invoked with some of the available JSON encode constants as its options parameter.
<?php
$input_array = array(
"zero",
"one",
"two"
);
// returns ["zero","one","two"]
$str_json_format = json_encode($input_array);
print "JSON Formatted String:" . $str_json_format;
// returns {"0":"zero","1":"one","2":"two"}
$obj_json_format = json_encode($input_array, JSON_FORCE_OBJECT);
print "<br/><br/>JSON Object:" . $obj_json_format;
// returns [ "zero", "one", "two" ]
$strJsonFormat_with_space = json_encode($input_array, JSON_PRETTY_PRINT);
print "<br/><br/>JSON Formatted String with white space:" . $strJsonFormat_with_space;
?>
Note:
This is the reverse operation of JSON encode, obviously used to convert JSON encoded data into its original PHP data type from where it is encoded initially.
For that, the second method json_decode() we have listed at the beginning of this article, will be used. This function accepts four arguments as listed below.
The third and fourth arguments are the same as we have seen for json_encode(). The default value for the depth limit is 512. And, the PHP supported constantly for the option parameter of this function is, JSON_BIGINT_AS_STRING, which is used to convert long integers into float data.
With the above PHP example program, we need to append the following lines.
<?php
$str_json_array_decoded = json_decode($str_json_format);
print "<br/><br/>Resultant decoded array from JSON array:<br/>";
print "<PRE>";
print_r($str_json_array_decoded);
print "</PRE>";
$str_objJson_decoded = json_decode($obj_json_format);
print "<br/><br/>Resultant decoded object data from JSON object:<br/>";
print "<PRE>";
print_r($str_objJson_decoded);
print "</PRE>";
$str_jsonAry_decoded = json_decode($obj_json_format, true);
print "<br/><br/>Resultant decoded array data from JSON object:<br/>";
print "<PRE>";
print_r($str_jsonAry_decoded);
print "</PRE>";
?>
After that, on executing this code, the following output will be returned to the browser.
Resultant decoded array from JSON array:
Array
(
[0] => zero
[1] => one
[2] => two
)
Resultant decoded object data from JSON object:
stdClass Object
(
[0] => zero
[1] => one
[2] => two
)
Resultant decoded array data from JSON object:
Array
(
[0] => zero
[1] => one
[2] => two
)
Note:
Download PHP JSON Encode Decode Source Code
Best regards from Brazil!!!
Great job in write this article!!!
Remembering that somethings parameters works in PHP 5.4!!
Thank’s
Great tuto
nice article i think this is the best of others