PHP json_decode() Function

PHP json_decode is a function that converts a JSON string into a PHP value. It can return either an object or an associative array, depending on the second parameter.

The syntax of the json_decode() function is as follows:

 

$phpValue = json_decode($jsonString, $assoc = false, $depth = 512, $options = 0);

  • $jsonString: The JSON string to be decoded.
  • $assoc: (Optional) Whether to return an associative array (true) or an object (false). The default value is false.
  • $depth: (Optional) The maximum nesting depth of the JSON structure. The default value is 512.
  • $options: (Optional) A bitwise OR of flags that control the decoding process. The default value is 0.

Note: The json_decode function is used to convert the JSON string to either an array or an object. The second parameter of json_decode is set to true for an array, and it's omitted or set to false for an object.

Example 1: Decode a Simple JSON Object

 
$jsonString = '{ "name": "John Doe", "age": 30 }';
$phpValue = json_decode($jsonString);
 
print_r($phpValue);
 
 
This code will output the following:
stdClass Object
(
    [name] => John Doe
    [age] => 30
)
 
 

Example 2: Decode a JSON Array

 

 
$jsonString = '[ "apple", "banana", "orange" ]';
$phpValue = json_decode($jsonString);
 
print_r($phpValue);
 
 
This code will output the following:
Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)
 
 

Example 3: Decode a JSON String with Nested Objects

 
$jsonString = '{ "name": "John Doe", "address": { "street": "123 Main St", "city": "Anytown", "state": "CA" } }';
 
$phpValue = json_decode($jsonString);
 
print_r($phpValue);
 
This code will output the following:
 
stdClass Object
(
    [name] => John Doe
    [address] => stdClass Object
        (
            [street] => 123 Main St
            [city] => Anytown
            [state] => CA
        )
)
 
 
 
 

Example 4: Convert JSON to an array and Convert JSON to an object

 
 
// JSON data for students
$json_students = '{"students": [{"name": "John", "age": 20}, {"name": "Alice", "age": 22}]}';
 
// Convert JSON to an array
$students_array = json_decode($json_students, true);
 
// Convert JSON to an object
$students_object = json_decode($json_students);
 
// Print the results
echo "Students Array:\n";
print_r($students_array);
 
echo "\nStudents Object:\n";
print_r($students_object);
 
 

Example 5: Set the Maximum Nesting Depth

 
 
$jsonString = '{ "name": "John Doe", "friends": [ { "name": "Jane Doe" }, { "name": "Peter Jones" } ], "address": { "street": "123 Main St", "city": "Anytown", "state": "CA" } }';
 
$phpValue = json_decode($jsonString, false, 1);
 
print_r($phpValue);
 
 
This code will output the following:
 
stdClass Object
(
    [name] => John Doe
    [friends] => stdClass Object
        (
            [0] => stdClass Object
                (
                    [name] => Jane Doe
                )
            [1] => stdClass Object
                (
                    [name] => Peter Jones
                )
        )
    [address] => stdClass Object
        (
            [street] => 123 Main St
            [city] => Anytown
            [state] => CA
        )
)