PHP json_encode() Equivalent in Python
Python's json.dumps() serializes to JSON just like PHP's json_encode(), but PHP's associative-vs-sequential array ambiguity (does it become a JSON object or array?) has no equivalent guesswork in Python, since dicts and lists are always distinct types.
Close equivalent Covers the common case, but has documented behavioral differences.
- PHP source
json_encode()- Closest Python API
json.dumps()
Main limitation: PHP arrays are a single type used for both lists and maps; `json_encode()` decides whether to emit a JSON array or object based on whether the array's keys are sequential integers starting at 0. Python's `json.dumps()` never has to guess — a `dict` always becomes a JSON object and a `list` always becomes a JSON array.
How to use each function
Before comparing the two, here's how json_encode() and json.dumps() work
on their own.
PHP: json_encode()
json_encode(mixed $value, int $flags = 0, int $depth = 512): string|false
Serializes a PHP value to a JSON string. Whether an array becomes a JSON object or array depends entirely on its keys — sequential integer keys starting at 0 produce a JSON array, anything else produces an object. Forward slashes and non-ASCII characters are escaped by default unless JSON_UNESCAPED_SLASHES / JSON_UNESCAPED_UNICODE are set.
Returns: A JSON string, or false on failure.
json_encode(["name" => "Ada", "age" => 30]);
// '{"name":"Ada","age":30}' Python: json.dumps()
json.dumps(obj, *, ensure_ascii=True, indent=None, ...)
Serializes a Python object to a JSON string. A dict always becomes a JSON object and a list always becomes a JSON array — there's no key-shape ambiguity like PHP's array. Non-ASCII characters are escaped by default; pass ensure_ascii=False to emit raw UTF-8.
Returns: A JSON-formatted string.
import json
json.dumps({"name": "Ada", "age": 30})
# '{"name": "Ada", "age": 30}' Quick mapping
| PHP | json_encode() |
|---|---|
| Closest Python API | json.dumps() |
How it differs
- PHP arrays are a single type used for both lists and maps;
json_encode()decides whether to emit a JSON array or object based on whether the array's keys are sequential integers starting at 0. Python'sjson.dumps()never has to guess — adictalways becomes a JSON object and alistalways becomes a JSON array. - PHP's
json_encode()escapes forward slashes (/becomes\/) by default, requiring theJSON_UNESCAPED_SLASHESflag to disable it; Python'sjson.dumps()never escapes forward slashes — there is no equivalent flag needed because it never does this in the first place. - PHP's
json_encode()escapes non-ASCII Unicode characters as\uXXXXby default, needingJSON_UNESCAPED_UNICODEto output raw UTF-8; Python'sjson.dumps()does the same ASCII-escaping by default, and needsensure_ascii=Falseto output raw Unicode — both languages default to escaping, just with a different flag name.
Examples
Encoding a list (sequential array / list)
json_encode([1, 2, 3]);
// "[1,2,3]" json.dumps([1, 2, 3])
# '[1, 2, 3]' Encoding a map (associative array / dict)
json_encode(["name" => "Ada", "age" => 30]);
// {"name":"Ada","age":30} json.dumps({"name": "Ada", "age": 30})
# '{"name": "Ada", "age": 30}' Edge cases
Edge case
An empty PHP array ([]) is ambiguous — json_encode([]) produces [] (a JSON array) because an empty array is considered sequential by definition; if you intended an empty object, you need json_encode((object) []) or json_encode([], JSON_FORCE_OBJECT). Python has no such ambiguity: json.dumps({}) is always {} and json.dumps([]) is always [].
References
- PHP Manual — json_encode — checked 2026-07-29
- Python docs — json.dumps() (v3.x) — checked 2026-07-29
Last verified 2026-07-29.