Customize JSON Array Decoding with array_hook in Python 3.15

Customize JSON Array Decoding with array_hook in Python 3.15

Decoding JSON produces Python objects chosen by the json module, and every JSON array becomes a list. That is a reasonable default, but a list is mutable and therefore cannot be hashed. A decoded array cannot be used as a dictionary key, stored in a set, or combined into a cache key without first being converted.

For example, a document describing a point decodes its coordinates as nested lists:

import json

data = json.loads('{"name":"point","coords":[[1,2],[3,4]]}')

print(data)  # {'name': 'point', 'coords': [[1, 2], [3, 4]]}
print(type(data['coords']).__name__)  # list

Converting the lists afterwards means walking the structure and rebuilding every array it contains, including the nested ones.

Since Python 3.15, the array_hook parameter controls how arrays are turned into Python objects.

import json

data = json.loads('{"name":"point","coords":[[1,2],[3,4]]}', array_hook=tuple)

print(data)  # {'name': 'point', 'coords': ((1, 2), (3, 4))}
print(type(data['coords']).__name__)  # tuple

The hook runs for every array in the document, so the two inner arrays become tuples as well as the outer one. The conversion happens while the document is being parsed, which removes the separate pass over the result.

The parameter accepts any callable, not only tuple. Passing frozenset produces sets of values instead, and a custom class gives every array a type of its own. The parameter is available on json.load, json.loads and the json.JSONDecoder constructor, so it works for both string and file input, and for decoders that are created once and reused.

The 2 Comments Found

  1. Avatar
    Lang Lang Reply

    One semantic caution with frozenset as array_hook: JSON arrays are ordered and may contain duplicates. [1, 1, 2] would collapse to two values, and [1, 2] would become indistinguishable from [2, 1]. tuple preserves order and duplicates, although hashability still depends on every converted child being hashable.

    • Avatar
      lindevs Reply

      You are right. frozenset loses the order and any duplicates, and a tuple is hashable only if all its values are hashable. array_hook leaves nested JSON objects as dicts, and dicts are not hashable.

Leave a Comment

Cancel reply

Your email address will not be published.