JSON Decode Online
Decode JSON and see exactly what PHP’s json_decode() gives you, as print_r or var_dump output, or as a tree. Works with escaped JSON strings too, and tells you why json_decode() would return NULL.
$data = json_decode($json, true);
Valid JSON · object with 6 keys · 2 levels deep · 163 bytes
$data = json_decode($json, true);
Array
(
[id] => 7
[name] => Maya Patel
[roles] => Array
(
[0] => editor
[1] => admin
)
[address] => Array
(
[city] => Leeds
[postcode] => LS1 4AP
)
[active] => 1
[manager] =>
)
Output matches PHP 8’s json_decode(), print_r() and var_dump(). Everything runs in your browser; nothing is uploaded.
What does json_decode() do?
json_decode() is PHP’s built-in function for reading JSON. It takes a JSON string, such as an API response or the contents of a .json file, and turns it into PHP values you can loop over and read. Its opposite is json_encode(), which turns PHP values back into JSON.
The second argument decides what JSON objects become. With true they are associative arrays, read with $data['name']. Without it they are stdClass objects, read with $data->name.
| JSON | Example | json_decode($json, true) | json_decode($json) |
|---|---|---|---|
| Object | {"city": "Leeds"} | array | stdClass object |
| Array | ["editor", "admin"] | array | array |
| String | "Maya" | string | string |
| Whole number | 7 | int | int |
| Decimal | 8.5, 1.0, 1e3 | float | float |
| true / false | true | bool | bool |
| null | null | NULL | NULL |
How to decode JSON online
- Paste the JSON or use Open file. If it is an escaped string full of
\", such as one copied from a log or a database column, select Unescape it. - Choose arrays or objects, to match whether your code passes
true. The line above the output shows the exact call. - Read the result as
print_r(short and readable) orvar_dump(shows every type and string length). - Open the Tree and click a value to get the PHP code that reads it, such as
$data['address']['city'].
If the JSON is invalid, the tool shows the line and column of the mistake, and the message json_last_error_msg() would give you in PHP.
Example: read a value from decoded JSON
A user API returns this profile (it is the example loaded in the tool), and you need the user’s city.
Step 1: Decode it and print it
Step 2: Follow the keys to the value
The answer is Leeds. Notice that print_r shows true as 1 and leaves null blank. Switch to var_dump to see bool(true) and NULL.
Why json_decode() returns NULL
json_decode() returns NULL when it can’t read the JSON, and it doesn’t throw an error by default. Call json_last_error_msg() straight after it to find out why:
| Message | Usual cause | Fix |
|---|---|---|
| Syntax error | Trailing comma, single quotes, comments, or an invisible BOM at the start of a file | Fix the mistake shown above; strip a BOM before decoding |
| Malformed UTF-8 characters, possibly incorrectly encoded | The text is not UTF-8, e.g. a file saved as Windows-1252 | mb_convert_encoding($json, 'UTF-8', 'Windows-1252') |
| Control character error, possibly incorrectly encoded | A real line break or tab inside a "text" value | Write \n and \t instead |
| Maximum stack depth exceeded | 512 or more levels of nesting | Pass a bigger depth as the third argument |
| No error | The JSON really was null | Use JSON_THROW_ON_ERROR to tell the two apart |
Since PHP 7.3 you can make it throw instead, which is the safer habit:
try {
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
echo 'Bad JSON: ' . $e->getMessage(); // e.g. "Syntax error"
}
// Remove a UTF-8 BOM first if the JSON comes from a file
$json = preg_replace('/^\xEF\xBB\xBF/', '', $json);json_decode() options and large numbers
The full signature is json_decode($json, $associative, $depth = 512, $flags = 0). These flags change the result:
| Flag | What it does |
|---|---|
| JSON_THROW_ON_ERROR | Throws a JsonException instead of returning NULL |
| JSON_BIGINT_AS_STRING | Keeps integers above 9223372036854775807 as strings instead of floats |
| JSON_OBJECT_AS_ARRAY | Same as passing true; used when $associative is null |
| JSON_INVALID_UTF8_SUBSTITUTE | Replaces bad bytes with � instead of failing |
| JSON_INVALID_UTF8_IGNORE | Drops bad bytes instead of failing |
PHP integers are 64-bit, so IDs up to 9,223,372,036,854,775,807 decode exactly. That is better than JavaScript, which starts losing digits after 9,007,199,254,740,991. Anything larger becomes a float: 12345678901234567890 prints as 1.2345678901235E+19. Use JSON_BIGINT_AS_STRING if you need the digits. The tool shows this option as soon as your JSON contains such a number.
Frequently asked questions
Should I use json_decode($json, true)?
Usually, yes. Associative arrays work with every array function (foreach, array_column, isset) and are what most PHP code expects. Objects are handy if you prefer $data->name, or need to tell an empty JSON object {} from an empty array [], which both become [] with true.
Why does print_r show 1 for true and nothing for false or null?
print_r turns values into text: true becomes "1", while false and null become an empty string. The data is fine. Use var_dump to see the real types: bool(true), bool(false) and NULL.
How do I decode an escaped JSON string?
Text like "{\"id\":7}" is JSON that was encoded twice, so it decodes to a string, not an array. Paste it here and select Unescape it, or in PHP call json_decode() twice: json_decode(json_decode($text), true).
What is the difference between json_decode and json_encode?
json_decode() reads JSON text into PHP arrays or objects. json_encode() does the reverse and writes PHP values out as JSON text, for example to send in an API response.
Sources
- PHP manual: json_decode: arguments, return values and flags
- PHP manual: json_last_error_msg: why decoding failed
- PHP manual: JSON constants: JSON_THROW_ON_ERROR, JSON_BIGINT_AS_STRING and the rest
- PHP manual: print_r: how values are printed
- PHP manual: var_dump: types and string lengths
- RFC 8259: The JSON Data Interchange Format: what counts as valid JSON
Related Tools
Build and preview Valorant crosshair settings.
Search common Vim commands.
Turn JSON rows into bar, line, or pie charts.
Convert WebP files and Base64 data in both directions.
Convert GIF files and Base64 data in both directions.
Save any GIF frame, or all frames, as JPG.