json_decode

FunctionParams and return types changed in PHP 8.0

Decodes a JSON string.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2-8.3
PHP 8.4
PHP 8.5
PHP 8.6
Improved

json_decode Function synopsis

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

Parameters

$json

Typestring

The $json string being decoded.

$associative

OptionalType?boolDefault valuenull

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

OptionalTypeintDefault value512

Maximum nesting depth of the structure being decoded. The value must be greater than 0, and less than or equal to 2147483647.

$flags

OptionalTypeintDefault value0

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.

$flags accepts one or a bitmask of the following constant values:

Return value

Typemixed

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.

Changes to the json_decode Function

PHP 8.6

PHP 8.0

  • Return type added: mixed
  • Parameter type added for parameter #1 ($json): string
  • Parameter name of parameter #2 changed: $assoc to $associative
  • Parameter type added for parameter #2 ($associative): ?bool
  • Parameter default value added for position #2 ($associative): null
  • Parameter type added for parameter #3 ($depth): int
  • Parameter default value added for position #3 ($depth): 512
  • Parameter name of parameter #4 changed: $options to $flags
  • Parameter type added for parameter #4 ($flags): int
  • Parameter default value added for position #4 ($flags): 0
  json_decode(
-     $json,
+     string $json,
-     $assoc,
+     ?bool $associative = null,
-     $depth,
+     int $depth = 512,
-     $options
+     int $flags = 0
-   )
+   ): mixed

PHP 5.4

  • Optional parameter added - #4: $options
 json_decode(
     $json,
     $assoc,
-    $depth
+    $depth,
+    $options
   )

json_decode Function Availability

PHP VersionAvailability
PHP 8.6Upcoming Release Yes
PHP 8.5Supported (Latest) Yes
PHP 8.4Supported Yes
PHP 8.3Security-Fixes Only Yes
PHP 8.2Security-Fixes Only Yes
PHP 8.1Unsupported Yes
PHP 8.0Unsupported Yes
PHP 7.4Unsupported Yes
PHP 7.3Unsupported Yes
PHP 7.2Unsupported Yes
PHP 7.1Unsupported Yes
PHP 7.0Unsupported Yes
PHP 5.6Unsupported Yes
PHP 5.5Unsupported Yes
PHP 5.4Unsupported Yes
PHP 5.3Unsupported Yes