Flatten nested JSON/dicts into dot-notation keys and unflatten back
Project description
philiprehberger-flatten-json
Flatten nested JSON/dicts into dot-notation keys and unflatten back.
Installation
pip install philiprehberger-flatten-json
Usage
from philiprehberger_flatten_json import flatten, unflatten
nested = {"a": {"b": {"c": 1}}, "d": [2, 3]}
flatten(nested)
# {"a.b.c": 1, "d.0": 2, "d.1": 3}
unflatten({"a.b.c": 1, "d.0": 2, "d.1": 3})
# {"a": {"b": {"c": 1}}, "d": [2, 3]}
# Custom separator
flatten(nested, separator="/")
# {"a/b/c": 1, "d/0": 2, "d/1": 3}
# Max depth
flatten(nested, max_depth=1)
# {"a": {"b": {"c": 1}}, "d": [2, 3]}
# Prefix
flatten(nested, prefix="root")
# {"root.a.b.c": 1, "root.d.0": 2, "root.d.1": 3}
# Keep numeric keys as dict
unflatten({"a.0": 1, "a.1": 2}, list_as_dict=True)
# {"a": {"0": 1, "1": 2}}
API Reference
| Function | Description |
|---|---|
flatten(data, *, separator=".", max_depth=0, prefix="") |
Flatten nested dict/list into dot-notation keys |
unflatten(data, *, separator=".", list_as_dict=False) |
Restore nested structure from flat dict |
Parameters
flatten()
data— Nested dict or listseparator— Key separator (default".")max_depth— Max depth to flatten, 0 = unlimitedprefix— String to prepend to all keys
unflatten()
data— Flat dict with composite keysseparator— Key separator used during flatteninglist_as_dict— WhenTrue, numeric keys stay as dict keys instead of converting to lists
Development
pip install -e .
python -m pytest tests/ -v
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_flatten_json-0.2.3.tar.gz.
File metadata
- Download URL: philiprehberger_flatten_json-0.2.3.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aaaa1db923be2e60ff21b4296a3b7c4eb3cf703536899afa4db341030709a108
|
|
| MD5 |
b72ebb0be80750d95df139b710bb716a
|
|
| BLAKE2b-256 |
3e2e84b2c5fb01091ef4a5fae9307d9fd43dd31d17c8e03a898dd68df36cc68a
|
File details
Details for the file philiprehberger_flatten_json-0.2.3-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_flatten_json-0.2.3-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
19617dfc12a19dcc67ed7e43b131ca224bbe768be1270362f60dd0dbd85e777e
|
|
| MD5 |
bb1ddfff14a0a6d1cdf28ec31b496039
|
|
| BLAKE2b-256 |
578ae23d622cc3f3230ec01018a20cc88f8f28c9e744649970afaee47513ce74
|