json_decode

PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL json >= 1.2.0
json_decode - Decodes a JSON string

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

Takes a JSON encoded string and converts it into a PHP variable.

Parameters

json

The json string being decoded.

This function only works with UTF-8 encoded strings.

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.

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 in appropriate PHP type. 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.

Notes

Note:

The JSON spec is not JavaScript, but a subset of JavaScript.

Note:

In the event of a failure to decode, json_last_error can be used to determine the exact nature of the error.

Changelog

Version Description
7.3.0 JSON_THROW_ON_ERROR flags was added.
7.2.0 associative is nullable now.
7.2.0 JSON_INVALID_UTF8_IGNORE, and JSON_INVALID_UTF8_SUBSTITUTE flags were added.
7.1.0 An empty JSON key ("") can be encoded to the empty object property instead of using a key with value _empty_.

Related Functions

Example of json_decode

Show all examples for json_decode

PHP Version:


Function json_decode:

JavaScript Object Notation Functions

Most used PHP functions