unflatten

raw JSON →
0.2.0 verified Fri May 01 auth: no python

Unflatten dicts to dicts with nested dicts/arrays. Current version 0.2.0. Release cadence: low (last release ~2021).

pip install unflatten
error AttributeError: module 'unflatten' has no attribute 'unflatten'
cause Trying to call unflatten.unflatten() after doing `import unflatten` instead of `from unflatten import unflatten`.
fix
Use: from unflatten import unflatten
error KeyError: 'a.b.c'
cause Using default delimiter '/' but keys contain dots (or vice versa). Version 0.2.0 changed default delimiter to '/'.
fix
Specify delimiter: unflatten(flat, delimiter='.')
error TypeError: unflatten() got an unexpected keyword argument 'splitter'
cause splitter argument is available only in version 0.2.0+. Older version does not support it.
fix
Upgrade to 0.2.0: pip install --upgrade unflatten
breaking Version 0.2.0 changed default delimiter from '.' to '/'. To use dot notation, pass delimiter='.' explicitly.
fix Use unflatten(flat, delimiter='.') with dot-separated keys.
gotcha The library does not automatically handle integer keys for list indices. You must specify a 'splitter' that returns ints or parse manually.
fix Example: from unflatten import split, unflatten; unflatten({'a.0.b': 1}, splitter=lambda s: [int(p) if p.isdigit() else p for p in split(s, '.')])
deprecated Using 'None' as a value in the flat dict may cause unexpected behavior (treated as missing). This behavior is undocumented and may change.
fix Avoid None values in input dict or handle separately.

Basic usage: unflatten a flat dict with dot-separated keys into nested dict.

from unflatten import unflatten
flat = {'a.b.c': 1, 'a.b.d': 2, 'a.e': 3}
result = unflatten(flat)
print(result)