W3docs

PHP sandbox

PHP json_decode() Online

Decode JSON the way PHP does: arrays or stdClass objects, big integers, depth limits and json_last_error_msg(). Runs on real PHP 8.2 on our server when you press Run.

json_decode(string $json, ?bool $associative = null, int $depth = 512, int $flags = 0): mixed

Runs on our server: your input is sent to w3docs and executed by PHP 8.2 in a sandbox with no network. Do not paste passwords or personal data.

Samples

Used byte for byte: no trimming, no BOM stripping.

$associative
$flags

71 / 6,000 bytes of input · limit 6,000 bytes (about 6,000 characters)

Result

Press Run (or Ctrl+Enter) to call json_decode() on PHP 8.2. The typed result appears here.

Equivalent PHP

<?php
$result = json_decode('{"id":42,"tags":["php","json"],"price":9.99,"active":true,"owner":null}', true);
var_dump($result);

About json_decode()

json_decode() parses a JSON string into PHP values. With $associative = true objects become associative arrays; with false (the default) they become stdClass objects.

On invalid JSON it returns null without a warning, so the sandbox also shows json_last_error_msg(). Add JSON_THROW_ON_ERROR to get a JsonException instead.

Integers too large for 64 bits become floats unless you pass JSON_BIGINT_AS_STRING. The input is used byte for byte: no trimming and no BOM stripping.

Worked example

Objects as arraysphp
$json = '{"a":1,"b":[1,2.5,{"c":null}]}';
$data = json_decode($json, true);
echo $data['b'][1]; // 2.5
Detecting invalid JSONphp
try {
    json_decode("{'a':1}", true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
    echo $e->getMessage(); // Syntax error
}

Frequently asked questions

Why does json_decode() return null?

Either the JSON is literally null or decoding failed. Check json_last_error_msg(): common causes are single quotes, trailing commas, comments and invalid UTF-8.

How do I keep large integers exact?

Pass JSON_BIGINT_AS_STRING as a flag; numbers beyond PHP_INT_MAX then come back as strings.

What does the depth argument do?

It is the maximum nesting depth. Deeper input fails with "Maximum stack depth exceeded". A depth of 0 or less throws a ValueError.

Is my input sent to a server?

Yes. These pages run real PHP 8.2 on the w3docs server, so the text you enter is sent there to run, in a sandbox with no network access. Do not paste passwords, keys or personal data.

How much input can I send?

Up to 6,000 bytes across all text fields, which is about 6,000 characters of plain text (accented letters and emoji take 2 to 4 bytes). Larger input is refused before anything is sent.