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}}
Roundtrip
from philiprehberger_flatten_json import flatten, unflatten
data = {"users": [{"name": "Alice", "age": 30}], "meta": {"version": 2}}
flat = flatten(data)
# {"users.0.name": "Alice", "users.0.age": 30, "meta.version": 2}
restored = unflatten(flat)
# {"users": [{"name": "Alice", "age": 30}], "meta": {"version": 2}}
Path access
from philiprehberger_flatten_json import get_by_path, set_by_path
data = {"users": [{"name": "ada"}, {"name": "bob"}]}
get_by_path(data, "users.1.name")
# "bob"
get_by_path(data, "users.5.name", default="unknown")
# "unknown"
config: dict = {}
set_by_path(config, "db.host", "localhost")
set_by_path(config, "db.port", 5432)
# config == {"db": {"host": "localhost", "port": 5432}}
API
| 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 |
get_by_path(data, path, *, separator=".", default=None) |
Look up a value by dot-notation path |
set_by_path(data, path, value, *, separator=".") |
Set a value at path, creating intermediate dicts as needed |
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
Support
If you find this project useful:
License
Metadata
Release files for philiprehberger-flatten-json 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| philiprehberger_flatten_json-0.3.0.tar.gz | 187.0 kB | Without provenance | Details for philiprehberger_flatten_json-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| philiprehberger_flatten_json-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for philiprehberger_flatten_json-0.3.0-py3-none-any.whl |
Total release size: 192.5 kB
Release files / philiprehberger_flatten_json-0.3.0.tar.gz
| Download URL | philiprehberger_flatten_json-0.3.0.tar.gz |
|---|---|
| Size | 187.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5b6db9ff364cb3840649f00606de54d1b72973ab06c1e2f1edd4641979eb7a36
|
|
BLAKE2b-256 checksum How to use checksums |
ec60d68e3de586e7730f1354cecc3449a156fddf78ddf787c806ab0d3a2aaaa9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / philiprehberger_flatten_json-0.3.0-py3-none-any.whl
| Download URL | philiprehberger_flatten_json-0.3.0-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2263430caa8137fa85eedf5beb07d24f36c9f4ed533489c2877b2deab676cb9a
|
|
BLAKE2b-256 checksum How to use checksums |
ff1ac5ca1f204de070f79175bbe2da58b6d788050ac0e310063d37927f36aa97
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Security for philiprehberger-flatten-json 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.