Skip to main content

Round-trip-safe structural reshaping between nested and flat data: flatten, unflatten, and nest flat rows.

Project description

flatmonkey

Round-trip-safe structural reshaping between nested and flat data — flatten nested dicts/JSON into {path: value}, unflatten back, and nest flat rows into nested records. A lightweight, round-tripping alternative to pandas.json_normalize.

Flattened keys are valid grabmonkey paths, so grab(data, key) works on any key flatmonkey produces.

Install

pip install flatmonkey

Quick start

from flatmonkey import flatten, unflatten, flatten_all, nest

d = {"user": {"name": "Ada", "roles": ["admin", "dev"]}}
flat = flatten(d)
# {'user.name': 'Ada', 'user.roles[0]': 'admin', 'user.roles[1]': 'dev'}

unflatten(flat) == d          # True — the round trip holds

# Batch:
flatten_all([{"a": {"b": 1}}, {"a": {"b": 2}}])
# [{'a.b': 1}, {'a.b': 2}]

# Nest flat rows into nested records:
nest(
    [{"order_id": 1, "product": "x", "qty": 2},
     {"order_id": 1, "product": "y", "qty": 1}],
    group_by=["order_id"],
    nest_fields={"items": ["product", "qty"]},
)
# [{'order_id': 1, 'items': [{'product': 'x', 'qty': 2}, {'product': 'y', 'qty': 1}]}]

Options

  • sep="/" — choose a separator your dict keys do not contain (any single char except [ / ]).
  • list_handling="index" | "stringify" | "omit" | "keep" — how list values are rendered. Only the default "index" round-trips.
  • max_depth=N — stop flattening after N path segments; deeper structure is kept as a value.

CLI

flatmonkey flatten data.json -o flat.json
flatmonkey unflatten flat.json
flatmonkey nest rows.json --group-by order_id --nest items=product,qty
cat data.json | flatmonkey flatten -        # '-' reads stdin / writes stdout

Using with AI assistants

See SKILL.md for an LLM-consumable reference (decision tree, worked examples, troubleshooting).

Design tradeoffs

Some behaviours are intentional and might look surprising — see LIMITATIONS.md for the deliberate design decisions (tuple/int-key lossiness, one-way list modes, collision handling, nest carry-through).

License

MIT — see LICENSE.

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

flatmonkey-0.1.0.tar.gz (32.0 kB view details)

Uploaded Source

Built Distribution

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

flatmonkey-0.1.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file flatmonkey-0.1.0.tar.gz.

File metadata

  • Download URL: flatmonkey-0.1.0.tar.gz
  • Upload date:
  • Size: 32.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for flatmonkey-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1f46db03630d2cc59528d0171dac08c8af0667c2ce3de4b0ef520fb2e43862ae
MD5 a6f68fc25f20103ee953bf82ec7eac8a
BLAKE2b-256 24fbea158ba835b585cdc903af770a9a6ad51878303e6515a5e2612b8245a7c2

See more details on using hashes here.

File details

Details for the file flatmonkey-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: flatmonkey-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for flatmonkey-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 60b223f60341dcbe7a42f31b1ddf08b701fcb2b1dd902b838f2e3fbc22d1e376
MD5 c04b0f04295111748d00b81b7fff2de2
BLAKE2b-256 75d6ee9344f7f65bf020525adf5a286581004db186741d53d446ff3d83dc460b

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