PHP | json_decode() Function Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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 <?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)); ?> Output: 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 <?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'}; ?> Output: 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: https://www.php.net/manual/en/function.json-decode.php Comment More info C Code_Mech Follow Improve Article Tags : Web Technologies PHP PHP-function Explore PHP Tutorial 8 min read BasicsPHP Syntax 4 min read PHP Variables 5 min read PHP | Functions 8 min read PHP Loops 4 min read ArrayPHP Arrays 5 min read PHP Associative Arrays 4 min read Multidimensional arrays in PHP 5 min read Sorting Arrays in PHP 4 min read OOPs & InterfacesPHP Classes 2 min read PHP | Constructors and Destructors 5 min read PHP Access Modifiers 4 min read Multiple Inheritance in PHP 4 min read MySQL DatabasePHP | MySQL Database Introduction 4 min read PHP Database connection 2 min read PHP | MySQL ( Creating Database ) 3 min read PHP | MySQL ( Creating Table ) 3 min read PHP AdvancePHP Superglobals 6 min read PHP | Regular Expressions 12 min read PHP Form Handling 4 min read PHP File Handling 4 min read PHP | Uploading File 3 min read PHP Cookies 9 min read PHP | Sessions 7 min read Like