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.

PHP
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.

Python
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's json.dumps() never has to guess — a dict always becomes a JSON object and a list always becomes a JSON array.
  • PHP's json_encode() escapes forward slashes (/ becomes \/) by default, requiring the JSON_UNESCAPED_SLASHES flag to disable it; Python's json.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 \uXXXX by default, needing JSON_UNESCAPED_UNICODE to output raw UTF-8; Python's json.dumps() does the same ASCII-escaping by default, and needs ensure_ascii=False to output raw Unicode — both languages default to escaping, just with a different flag name.

Examples

Encoding a list (sequential array / list)

PHP
json_encode([1, 2, 3]);
// "[1,2,3]"
Python
json.dumps([1, 2, 3])
# '[1, 2, 3]'
Both produce a JSON array; Python adds spaces after commas by default, which is a formatting difference, not a structural one.

Encoding a map (associative array / dict)

PHP
json_encode(["name" => "Ada", "age" => 30]);
// {"name":"Ada","age":30}
Python
json.dumps({"name": "Ada", "age": 30})
# '{"name": "Ada", "age": 30}'
PHP's associative array becomes a JSON object because its keys aren't sequential integers; Python's dict is unambiguously always a JSON object, with no key-shape inspection involved.

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.