Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Functions and Classes

PHP json_decode() function

Reference for PHP json_decode(): parse JSON text into arrays or objects, handle null and syntax errors, big integers, depth limits and JSON_THROW_ON_ERROR.

json_decode() parses a JSON string and returns the matching PHP value. JSON objects become stdClass objects by default, or associative arrays when you ask for them; JSON arrays become PHP lists; numbers, strings, booleans and null become the PHP scalars you would expect.

The classic trap is that null is both a valid result (the JSON text null) and the failure value. Pass JSON_THROW_ON_ERROR so invalid input throws a JsonException instead of being silently turned into null.

PHP
$data = json_decode('{"name":"Ada","tags":["math"]}', true, 512, JSON_THROW_ON_ERROR);
echo $data["name"]; // Ada

Try it

PHP Objects, arrays and types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Detecting errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
json_decode(
    string $json,
    ?bool $associative = null,
    int $depth = 512,
    int $flags = 0
): mixed
Name Description
$json The JSON text. It must be valid UTF-8 and strict JSON.
$associative true returns arrays for JSON objects, false returns stdClass objects. null (default) follows JSON_OBJECT_AS_ARRAY in $flags.
$depth Maximum nesting depth, greater than zero. Deeper input is an error.
$flags A bitmask: JSON_OBJECT_AS_ARRAY, JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_THROW_ON_ERROR.

Return value: the decoded value (array, object, string, int, float, bool or null). On failure it returns null and sets the error state read by json_last_error() and json_last_error_msg().

Errors: JsonException with JSON_THROW_ON_ERROR; a ValueError if $depth is not positive or is too large.

Usage notes

  • Use JSON_THROW_ON_ERROR (PHP 7.3+). Otherwise you must call json_last_error() after every decode, because null could be valid output.
  • JSON is strict. Single quotes, unquoted keys, comments, trailing commas and an empty string are all syntax errors. Only lowercase true, false and null are accepted.
  • Objects or arrays? Pass true as the second argument for associative arrays, which are easier to loop over and test with isset(). Use the object form when you want -> access.
  • Numeric keys: {"1":"a"} decoded as an array gives integer keys, as the first example shows; as an object the property name is the string "1".
  • Big integers that do not fit in PHP_INT_MAX become floats and lose precision. Use JSON_BIGINT_AS_STRING to keep every digit.
  • Duplicate keys: the last one wins.
  • Validate the shape after decoding. A decoded value can be a scalar even when you expected an object; check with is_array() as in the second example, or use filter_var() on fields.
  • Depth counts nesting levels of arrays and objects; the default 512 is plenty, and lowering it is a cheap guard against hostile input.
  • Decoding does not run code. Unlike unserialize(), it creates only stdClass, arrays and scalars, so it is safe for untrusted input (it still needs validation).
  • An empty string is a syntax error since PHP 7.0: json_decode("") fails.