Convert JSON to PHP arrays or objects online. Preview how PHP will represent any JSON structure, understand json_decode() output, and generate ready-to-use PHP code. Free, no signup required.
PHP has had built-in JSON support since PHP 5.2 through two core functions: json_encode() and json_decode(). Understanding how json_decode() maps JSON types to PHP types is fundamental for any PHP developer working with APIs, configuration, or data storage.
The function signature is:
json_decode(
string $json,
bool $associative = false,
int $depth = 512,
int $flags = 0
): mixed
The most important parameter is the second one, $associative. When false (the default), JSON objects are returned as stdClass instances. When true, JSON objects are returned as PHP associative arrays. JSON arrays are always returned as PHP indexed arrays regardless of this parameter.
| JSON type | PHP type (default) | PHP type (assoc=true) |
|---|---|---|
| string | string | string |
| number (int) | int | int |
| number (float) | float | float |
| boolean | bool | bool |
| null | null | null |
| object | stdClass | array (associative) |
| array | array (indexed) | array (indexed) |
The choice between associative arrays and stdClass objects is one of the first decisions you make when working with json_decode() in PHP.
stdClass objects are accessed with the arrow operator:
$data = json_decode($json); // returns stdClass
echo $data->name;
echo $data->address->city;
foreach ($data->items as $item) {
echo $item->price;
}
Associative arrays are accessed with bracket notation and support all PHP array functions:
$data = json_decode($json, true); // returns array
echo $data['name'];
echo $data['address']['city'];
$prices = array_column($data['items'], 'price');
$total = array_sum($prices);
For most PHP applications, associative arrays are the practical choice. They work with array_map, array_filter, array_column, and other standard PHP array functions. Framework libraries like Laravel prefer arrays. Use stdClass only when you need to interoperate with code that explicitly expects objects.
PHP's json_decode() returns null on failure without throwing an exception by default. This silent failure causes many hard-to-debug issues. Here are the most common errors and their solutions:
mb_convert_encoding($string, 'UTF-8', 'auto').$depth limit (default 512). Increase the depth parameter if needed.$json = ltrim($json, "\xef\xbb\xbf")."null" to decode as PHP null, which is indistinguishable from a decode error. Use json_last_error() to differentiate.In PHP 7.3+, the cleanest approach is to always pass JSON_THROW_ON_ERROR:
try {
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
// Handle the specific error
error_log('JSON decode failed: ' . $e->getMessage());
}
Following these practices ensures reliable and maintainable JSON handling in PHP applications:
application/json and that the body is non-empty before calling json_decode().\uXXXX sequences, which doubles payload size.application/json whenever outputting JSON from a PHP script or API endpoint.By default, json_decode() returns a stdClass object for JSON objects and a PHP indexed array for JSON arrays. Numbers map to int or float, strings map to string, booleans map to bool, and JSON null maps to PHP null. If the JSON string is invalid, the function returns null and sets a JSON error code retrievable via json_last_error(). Pass true as the second argument to get associative arrays instead of stdClass for all JSON objects.
With the default mode, JSON objects become stdClass and properties are accessed with $obj->key. With the associative mode (json_decode($json, true)), JSON objects become PHP arrays and values are accessed with $arr['key']. Associative arrays integrate naturally with PHP's standard library functions like array_map, array_filter, array_keys, and array_column. Most PHP frameworks and codebases prefer associative arrays for their flexibility and compatibility.
After calling json_decode(), use json_last_error() to get the error code and json_last_error_msg() for a human-readable message. Check if (json_last_error() !== JSON_ERROR_NONE) before using the decoded value. In PHP 7.3 and later, the cleanest approach is to pass JSON_THROW_ON_ERROR as the flags parameter, which causes json_decode() to throw a JsonException on failure instead of returning null silently.
The most important practice is to use JSON_THROW_ON_ERROR in PHP 7.3+ so invalid JSON never silently returns null and corrupts your application logic. Always set Content-Type: application/json when outputting JSON. Use JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES with json_encode() to produce clean, compact output. In PHP 8.0+ with typed properties, consider using a proper deserialization library like the Symfony Serializer or JMS Serializer to map JSON directly into typed PHP objects instead of working with untyped arrays.
Free, instant, 100% private. No account needed.
Open JSON to PHP Converter →Also useful: JWT Decoder | JSON Validator | JSON Formatter | JSON to Java | JSON to Go | JSON to C#