Syntax
The syntax of the json_decode() function in PHP is as follows:
json_decode(string $json, bool $associative = false, int $depth = 512, int $flags = 0): mixed
Parameters
The json_decode() function accepts the following parameters:
- $json (Required): The JSON string that needs to be decoded.
- $associative (Optional): A boolean value that determines whether the JSON object should be converted into an associative array (true) or a PHP object (false, default).
- $depth (Optional): The maximum depth of the nested structure to be decoded. The default value is 512.
- $flags (Optional): A bitmask of JSON decoding options. The default value is 0.
Return Values
- Returns a PHP object or an associative array, depending on the $associative parameter.
- Returns NULL if decoding fails or the JSON string is invalid.
- Returns false when JSON_BIGINT_AS_STRING is set and a big integer is encountered.
Examples
Example 1: Decoding a JSON String into an Object
<?php
$jsonString = '{"name": "John", "age": 25, "city": "New York"}';
$decodedObject = json_decode($jsonString);
// Accessing object properties
echo "Name: " . $decodedObject->name . "\n";
echo "Age: " . $decodedObject->age . "\n";
echo "City: " . $decodedObject->city . "\n";
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Name: John
Age: 25
City: New York
Example 2: Decoding a JSON String into an Associative Array
<?php
$jsonString = '{"name": "John", "age": 25, "city": "New York"}';
$decodedArray = json_decode($jsonString, true);
// Accessing array elements
echo "Name: " . $decodedArray["name"] . "\n";
echo "Age: " . $decodedArray["age"] . "\n";
echo "City: " . $decodedArray["city"] . "\n";
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Name: John
Age: 25
City: New York
Example 3: Handling Nested JSON Data
<?php
$jsonString = '{"person": {"name": "Alice", "age": 30, "address": {"city": "Los Angeles", "zip": "90001"}}}';
$decodedObject = json_decode($jsonString);
echo "Name: " . $decodedObject->person->name . "\n";
echo "City: " . $decodedObject->person->address->city . "\n";
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Name: Alice
City: Los Angeles
Common Errors while using json_decode() function
1. Invalid JSON Format
If the JSON string is malformed, json_decode() will return NULL.
Example:
<?php
$jsonString = '{name: "John", age: 25, city: "New York"}'; // Incorrect format
$decoded = json_decode($jsonString);
var_dump($decoded);
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
NULL
Solution: Ensure that keys and string values are enclosed in double quotes.
2. Exceeding Depth Limit
By default, PHP allows up to 512 levels of depth. If this limit is exceeded, an error occurs.
Example:
<?php
$jsonString = str_repeat('{"level":', 600) . '"end"' . str_repeat('}', 600);
$decoded = json_decode($jsonString);
if (json_last_error() !== JSON_ERROR_NONE) {
echo "Error: " . json_last_error_msg();
}
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Error: Maximum stack depth exceeded
Solution: Adjust the depth parameter if needed, but avoid excessive nesting.
3. Handling JSON Errors
PHP provides json_last_error() and json_last_error_msg() to diagnose JSON decoding issues.
Example:
<?php
$jsonString = '{"name": "John", "age": 25,}'; // Trailing comma
json_decode($jsonString);
if (json_last_error() !== JSON_ERROR_NONE) {
echo "JSON Decode Error: " . json_last_error_msg();
}
?>

You can also try this code with Online PHP Compiler
Run CodeOutput:
JSON Decode Error: Syntax error
Frequently Asked Questions
What does the json_decode() function do in PHP?
The json_decode() function in PHP is used to convert a JSON-formatted string into a PHP variable, either as an object or an associative array.
How do I convert a JSON string into an associative array in PHP?
You can set the second parameter of json_decode() to true:
$decodedArray = json_decode($jsonString, true);
This will convert the JSON string into an associative array.
How can I check if json_decode() failed?
You can use json_last_error() and json_last_error_msg() functions to detect decoding errors:
if (json_last_error() !== JSON_ERROR_NONE) {
echo "Error: " . json_last_error_msg();
}
Conclusion
The json_decode() function in PHP is a powerful tool for working with JSON data. It allows you to convert JSON strings into PHP objects or associative arrays, making it easier to handle API responses and structured data. Understanding how to use json_decode() effectively will help you avoid common errors and write more efficient PHP code.