json_decode — Decodes a JSON string
Description
json_decode(
string $json
,
?bool $associative
= null
,
int $depth
= 512,
int $flags
= 0
): mixed
Takes a JSON encoded string and converts it into a PHP value.
Parameters
json
The json
string being decoded.
This function only works with UTF-8 encoded strings.
Note:
PHP implements a superset of JSON as specified in the original » RFC 7159.
associative
When true
, JSON objects will be returned as associative arrays; when false
, JSON objects will be returned as objects. When null
, JSON objects will be returned as associative arrays or objects depending on whether JSON_OBJECT_AS_ARRAY
is set in the flags
.
depth
Maximum nesting depth of the structure being decoded. The value must be greater than 0
, and less than or equal to 2147483647
.
flags
Bitmask of JSON_BIGINT_AS_STRING
, JSON_INVALID_UTF8_IGNORE
, JSON_INVALID_UTF8_SUBSTITUTE
, JSON_OBJECT_AS_ARRAY
, JSON_THROW_ON_ERROR
. The behaviour of these constants is described on the JSON constants page.
Return Values
Returns the value encoded in json
as an appropriate PHP type. Unquoted values true
, false
and null
are returned as true
, false
and null
respectively. null
is returned if the json
cannot be decoded or if the encoded data is deeper than the nesting limit.
Errors/Exceptions
If depth
is outside the allowed range, a ValueError is thrown as of PHP 8.0.0, while previously, an error of level E_WARNING
was raised.
How to decode JSON into PHP values?
To convert JSON into PHP values (arrays, objects, etc.), just paste your JSON string in the editor and click the 'json_decode()' button. The corresponding PHP representation will appear in the editor below. As with the PHP version, you can provide the parameters associative
, depth
, and flags
.
Why decode JSON into PHP online?
The json_decode()
function in PHP lets you transform a JSON string into usable PHP data. When creating an API or loading data, it's often in JSON format. To work with this JSON data in PHP, we convert it into PHP values using the json_decode()
function, which allows us to manipulate the data directly in PHP.
why use an online tool for decoding? Honestly… no clue. But if you're here, chances are you need it!