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 afterNpath 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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f46db03630d2cc59528d0171dac08c8af0667c2ce3de4b0ef520fb2e43862ae
|
|
| MD5 |
a6f68fc25f20103ee953bf82ec7eac8a
|
|
| BLAKE2b-256 |
24fbea158ba835b585cdc903af770a9a6ad51878303e6515a5e2612b8245a7c2
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60b223f60341dcbe7a42f31b1ddf08b701fcb2b1dd902b838f2e3fbc22d1e376
|
|
| MD5 |
c04b0f04295111748d00b81b7fff2de2
|
|
| BLAKE2b-256 |
75d6ee9344f7f65bf020525adf5a286581004db186741d53d446ff3d83dc460b
|