The json_decode() function is an inbuilt function in PHP which is used to decode a JSON string. It converts a JSON encoded string into a PHP variable.
Syntax:
json_decode( $json, $assoc = FALSE, $depth = 512, $options = 0 )
Parameters: This function accepts four parameters as mentioned above and described below:
- json: It holds the JSON string which need to be decode. It only works with UTF-8 encoded strings.
- assoc: It is a boolean variable. If it is true then objects returned will be converted into associative arrays.
- depth: It states the recursion depth specified by user.
- options: It includes bitmask of JSON_OBJECT_AS_ARRAY, JSON_BIGINT_AS_STRING,, JSON_THROW_ON_ERROR.
Return values: This function returns the encoded JSON value in appropriate PHP type. If the json cannot be decoded or if the encoded data is deeper than the recursion limit then it returns NULL.
Below examples illustrate the use of json_decode() function in PHP:
Example 1:
<?php // Declare a json string $json = '{"g":7, "e":5, "e":5, "k":11, "s":19}'; // Use json_decode() function to // decode a string var_dump(json_decode($json)); var_dump(json_decode($json, true)); ?> |
object(stdClass)#1 (4) {
["g"]=>
int(7)
["e"]=>
int(5)
["k"]=>
int(11)
["s"]=>
int(19)
}
array(4) {
["g"]=>
int(7)
["e"]=>
int(5)
["k"]=>
int(11)
["s"]=>
int(19)
}
Example 2:
<?php // Declare a json string $json = '{"geeks": 7551119}'; // Use json_decode() function to // decode a string $obj = json_decode($json); // Display the value of json object print $obj->{'geeks'}; ?> |
7551119
Common Errors while using json_decode() function:
- Used strings are valid JavaScript but not valid JSON.
- Name and value must be enclosed in double quotes, single quotes are not allowed.
- Trailing commas are not allowed.
Reference: http://php.net/manual/en/function.json-decode.php
Recommended Posts:
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
- PHP | SimpleXMLElement children() Function
- PHP | array_intersect_ukey() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh

