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

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.1.tar.gz (4.6 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.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_flatten_json-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1a8a9b3698578c4ff0e59c0d255bde4f51269d1234b69e43dd5aa47fa26cfeb1
MD5 6ab8869713b6117372d6a74fde0c4bd5
BLAKE2b-256 4d84beeeb70515e90108adafbfc866ce67036c6e3c73bcdec3c5be30e231acb2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_flatten_json-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f247f34a16188c603881e1ad80effd1df7483da16d80268cb39dca112ba3717d
MD5 16440cec0c87b8273d0e9c71dd793093
BLAKE2b-256 27a397047d818fb8dee19647f5521f3261579f25e92e4fa310878b276d9e16f3

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