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.
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).
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)returnsstdClassobjects for JSON objects by default (accessed as$obj->name); passingtrueas the second argument returns associative arrays instead ($arr["name"]). Python'sjson.loads()always returns adictfor JSON objects — there is no object-vs-array mode to configure. - PHP's
json_decode()returnsnullon invalid JSON, and the failure reason must be checked separately withjson_last_error(); Python'sjson.loads()raises ajson.JSONDecodeError(a subclass ofValueError) directly, which is idiomatically caught withtry/exceptrather 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
json_decode('{"name":"Ada"}', true);
// ["name" => "Ada"] json.loads('{"name":"Ada"}')
# {'name': 'Ada'} Handling invalid JSON
json_decode("not json"); // null, check json_last_error() try:
json.loads("not json")
except json.JSONDecodeError as e:
... 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.