To convert JSON to a PHP array, call json_decode($json, true). The second argument, true, tells PHP to return associative arrays instead of stdClass objects, so you can read values with $data['key'].
That covers JSON you receive at runtime. When you want the data written into your source code instead, as a config file, seeder, test fixture or mock response, our free JSON to PHP array converter turns any JSON document into a ready-to-paste PHP short array. This guide explains both approaches, how JSON types map to PHP, and the edge cases that catch developers out.
How json_decode converts JSON to an array
In PHP 8 the function signature is:
json_decode(string $json, ?bool $associative = null, int $depth = 512, int $flags = 0): mixed
Pass true as the second argument to get arrays, and add the JSON_THROW_ON_ERROR flag (available since PHP 7.3) so invalid input throws a JsonException instead of quietly returning null:
<?php
$json = '{"name":"Ada","skills":["php","sql"],"address":{"city":"London"}}';
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
echo $data['name']; // Ada
echo $data['skills'][1]; // sql
echo $data['address']['city']; // London
Without true, JSON objects become stdClass objects and you read them with arrows instead: $data->address->city. Arrays are easier to loop over, merge and pass to most framework helpers, which is why true is the common choice.
How JSON types map to PHP
| JSON | PHP with json_decode($json, true) |
|---|---|
Object {"a": 1} |
Associative array ['a' => 1] |
Array [1, 2] |
Indexed array [1, 2] |
| String | string |
| Whole number | int, or float if it exceeds PHP_INT_MAX |
| Number with a fraction or exponent | float |
true / false |
bool |
null |
null |
Example: JSON to a PHP array literal
Paste this JSON into the converter:
{"name":"Ada","age":36,"price":19.99,"active":true,"manager":null,"skills":["php","sql"],"address":{"city":"London","zip":"N1 9GU"},"note":"It's $5"}
It returns this PHP:
[
'name' => 'Ada',
'age' => 36,
'price' => 19.99,
'active' => true,
'manager' => null,
'skills' => [
'php',
'sql',
],
'address' => [
'city' => 'London',
'zip' => 'N1 9GU',
],
'note' => 'It\'s $5',
]
Strings use single quotes, so $5 is never treated as a variable, and the apostrophe is escaped. JSON arrays become lists without keys, and every element ends with a trailing comma, which PHP allows and which keeps future diffs to one line.
How to use the JSON to PHP Array converter
- Open the JSON to PHP array converter and paste your JSON into Input JSON, up to 100,000 characters.
- The PHP short array panel updates automatically as you type, using 4-space indentation and lowercase
true,falseandnull. - If the input is not valid JSON, the panel shows "Invalid JSON" with the reason. Run the input through the JSON formatter to get the exact line and column of the problem.
- Click Copy and paste the array into your code. The output is the array literal only, so add
returnor a variable assignment and the closing semicolon yourself.
Where a PHP array literal is useful
- Config files that end with
return [ ... ];, such as Laravel'sconfig/*.php. - Database seeders that insert sample rows from an exported JSON file.
- Tests that compare a response to an expected array, for example with Laravel's
assertJson(), or that fake an HTTP API response.
Seed data often needs identifiers too. Generate a batch of unique IDs with the UUID generator and paste them into the array.
Common json_decode mistakes and edge cases
- Ignoring failures. Without
JSON_THROW_ON_ERROR,json_decode()returnsnullon invalid input, which looks the same as decoding the JSON literalnull. Use the flag, or checkjson_last_error_msg(). - Losing the difference between {} and []. With
true, an empty object and an empty array both become[], andjson_encode()turns that back into[]. If an API expects{}, sendnew stdClass()or use theJSON_FORCE_OBJECTflag. - Numeric string keys become integers. PHP stores the key
"1"as the integer1. An object with keys"0","1","2"becomes a list and re-encodes as a JSON array. - Very large integers lose precision. Numbers above 9,223,372,036,854,775,807 are converted to floats. Pass
JSON_BIGINT_AS_STRINGto keep them as strings, which our converter does not do. - Deep nesting. The default depth limit is 512 levels. Deeper documents fail to decode.
- Non-UTF-8 input. JSON must be UTF-8. Text saved in another encoding fails with a "Malformed UTF-8 characters" error.
Converting a PHP array back to JSON
Use json_encode(). For readable output that keeps slashes and accented characters as they are, combine flags:
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
If the data is headed for a YAML config instead, the JSON to YAML converter handles that step; our guide to converting JSON and YAML explains the type rules. For the JSON syntax itself, see how to format and validate JSON.
Frequently asked questions
How do I convert JSON to an array in PHP?
Call json_decode($json, true). The true argument returns nested associative arrays instead of stdClass objects.
What is the difference between json_decode with true and false?
With true, JSON objects become associative arrays read as $data['key']. With false or no argument, they become stdClass objects read as $data->key. JSON arrays become PHP arrays either way.
Why does json_decode return null?
It returns null when the JSON is invalid, nested deeper than the depth limit, or not valid UTF-8. Call json_last_error_msg() or use JSON_THROW_ON_ERROR to see the reason.
How do I write JSON as a PHP array in my code?
Paste the JSON into a JSON to PHP array converter and copy the generated short array syntax into your file. This is faster and less error-prone than rewriting brackets and arrows by hand.
Which PHP versions support short array syntax?
The [] short array syntax has been available since PHP 5.4, so the converter's output works in every PHP version still in use.
Does the converter execute my JSON as PHP?
No. The converter decodes the JSON on our server and builds the array as text; nothing is executed. Still, remove secrets and personal data before pasting production JSON into any online tool.