Skip to content

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.

What each JSON value becomes in PHP with json_decode()
JSONExamplejson_decode($json, true)json_decode($json)
Object{"city": "Leeds"}arraystdClass object
Array["editor", "admin"]arrayarray
String"Maya"stringstring
Whole number7intint
Decimal8.5, 1.0, 1e3floatfloat
true / falsetrueboolbool
nullnullNULLNULL

How to decode JSON online

  1. 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.
  2. Choose arrays or objects, to match whether your code passes true. The line above the output shows the exact call.
  3. Read the result as print_r (short and readable) or var_dump (shows every type and string length).
  4. 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.

{ "id": 7, "name": "Maya Patel", "roles": ["editor", "admin"], "address": { "city": "Leeds", "postcode": "LS1 4AP" }, "active": true, "manager": null }

Step 1: Decode it and print it

$data = json_decode($json, true); print_r($data); Array ( [id] => 7 [name] => Maya Patel [roles] => Array ( [0] => editor [1] => admin ) [address] => Array ( [city] => Leeds [postcode] => LS1 4AP ) [active] => 1 [manager] => )

Step 2: Follow the keys to the value

echo $data['address']['city']; // Leeds (with true) echo $data->address->city; // Leeds (without true)

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:

json_last_error_msg() messages, their causes and fixes
MessageUsual causeFix
Syntax errorTrailing comma, single quotes, comments, or an invisible BOM at the start of a fileFix the mistake shown above; strip a BOM before decoding
Malformed UTF-8 characters, possibly incorrectly encodedThe text is not UTF-8, e.g. a file saved as Windows-1252mb_convert_encoding($json, 'UTF-8', 'Windows-1252')
Control character error, possibly incorrectly encodedA real line break or tab inside a "text" valueWrite \n and \t instead
Maximum stack depth exceeded512 or more levels of nestingPass a bigger depth as the third argument
No errorThe JSON really was nullUse 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:

json_decode() flags and what they do
FlagWhat it does
JSON_THROW_ON_ERRORThrows a JsonException instead of returning NULL
JSON_BIGINT_AS_STRINGKeeps integers above 9223372036854775807 as strings instead of floats
JSON_OBJECT_AS_ARRAYSame as passing true; used when $associative is null
JSON_INVALID_UTF8_SUBSTITUTEReplaces bad bytes with � instead of failing
JSON_INVALID_UTF8_IGNOREDrops 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