Skip to main content

Flatten nested JSON/dicts into dot-notation keys and unflatten back

Project description

philiprehberger-flatten-json

Tests PyPI version License

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 list
  • separator — Key separator (default ".")
  • max_depth — Max depth to flatten, 0 = unlimited
  • prefix — String to prepend to all keys

unflatten()

  • data — Flat dict with composite keys
  • separator — Key separator used during flattening
  • list_as_dict — When True, numeric keys stay as dict keys instead of converting to lists

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_flatten_json-0.2.4.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_flatten_json-0.2.4-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_flatten_json-0.2.4.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_flatten_json-0.2.4.tar.gz
Algorithm Hash digest
SHA256 9f7e947b043533f5e19eef2b7f9ffee6e292e60d757cf02ac3840224d5884489
MD5 ce3da7b698ae03b534602da96b914083
BLAKE2b-256 f7da4aa939779ecddecef6189d71d70259692824e411a0c4a30b15f295e776fb

See more details on using hashes here.

File details

Details for the file philiprehberger_flatten_json-0.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_flatten_json-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bf5fe545f86c863fc7b7b17bf7863a0eeb64bef0acfd28c5bc075ff2258dcbcc
MD5 e43ce4b2c8aa0ef2d85ec7b8045835ff
BLAKE2b-256 f553e58b034f08d26b2e5d81c2c142aadd4689c351e25349fe6b93d1a5cddd49

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page