PHP json_decode() Equivalent in Python

Python's json.loads() parses JSON just like PHP's json_decode(), but PHP defaults to returning stdClass objects for JSON objects unless you pass true for associative arrays — Python's json.loads() always returns a plain dict, no flag needed.

Close equivalent Covers the common case, but has documented behavioral differences.

PHP source
json_decode()
Closest Python API
json.loads()

Main limitation: PHP's `json_decode($json)` returns `stdClass` objects for JSON objects by default (accessed as `$obj->name`); passing `true` as the second argument returns associative arrays instead (`$arr["name"]`). Python's `json.loads()` always returns a `dict` for JSON objects — there is no object-vs-array mode to configure.

How to use each function

Before comparing the two, here's how json_decode() and json.loads() work on their own.

PHP: json_decode()

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

Parses a JSON string into a PHP value. By default, JSON objects become stdClass instances accessed with ->; passing true for $associative returns nested associative arrays instead. Invalid input returns null, and the specific failure reason must be retrieved separately via json_last_error().

Returns: The decoded PHP value (array, stdClass, scalar, or null), or null on failure.

PHP
json_decode('{"name":"Ada"}', true);
// ["name" => "Ada"]

Python: json.loads()

json.loads(s)

Parses a JSON string (or bytes) into a Python value: objects become dict, arrays become list, and there's no associative-array flag to configure since the mapping is unambiguous. Invalid JSON raises json.JSONDecodeError directly at the call site instead of returning a sentinel value.

Returns: The decoded Python value (dict, list, str, int, float, bool, or None).

Python
import json
json.loads('{"name":"Ada"}')
# {'name': 'Ada'}

Quick mapping

PHP json_decode()
Closest Python API json.loads()

How it differs

  • PHP's json_decode($json) returns stdClass objects for JSON objects by default (accessed as $obj->name); passing true as the second argument returns associative arrays instead ($arr["name"]). Python's json.loads() always returns a dict for JSON objects — there is no object-vs-array mode to configure.
  • PHP's json_decode() returns null on invalid JSON, and the failure reason must be checked separately with json_last_error(); Python's json.loads() raises a json.JSONDecodeError (a subclass of ValueError) directly, which is idiomatically caught with try/except rather than checked via a side-channel function.
  • Both languages decode JSON numbers into their natural numeric types (int/float) and preserve key order from the source JSON — this part translates cleanly.

Examples

Decoding to an associative structure

PHP
json_decode('{"name":"Ada"}', true);
// ["name" => "Ada"]
Python
json.loads('{"name":"Ada"}')
# {'name': 'Ada'}
PHP needs the `true` second argument to get an associative array instead of a stdClass object; Python's json.loads() always returns a dict, with nothing extra to pass.

Handling invalid JSON

PHP
json_decode("not json"); // null, check json_last_error()
Python
try:
    json.loads("not json")
except json.JSONDecodeError as e:
    ...
PHP signals failure by returning null and requires a separate function call to learn why; Python raises a catchable exception directly at the call site.

Edge cases

Edge case

A literal JSON null decodes to PHP's null and Python's None — but PHP's json_decode() *also* returns null when the input is malformed, so a null result is ambiguous without checking json_last_error(). Python's exception-based approach never has this ambiguity: a None result unambiguously means the JSON literal was null.

References

  • PHP Manual — json_decode — checked 2026-07-29
  • Python docs — json.loads() (v3.x) — checked 2026-07-29

Last verified 2026-07-29.