dict2rel documentation#

dict2rel is a Python package aimed at aiding situations where you have highly nested JSON objects and want to either flatten them into a single table or transform them into multiple tables where nested lists get broken out into their own sheets; turning a dict into relational tables; and providing the name.

The library is table-type agnostic and therefore pandas.DataFrame, polars.DataFrame, or any other provider can be used when constructing the tables. The two previously mentioned types are automatically handled when converting back from tables into JSON.

>>> from dict2rel import dict2rel, UnravelOptions
>>> tables = dict2rel(
...     {
...         "project": "Alpha-Prime",
...         "version": "1.0.3",
...         "config": {
...             "modules": [
...                 {
...                     "id": "A1",
...                     "status": "active",
...                     "settings": {
...                         "security": {
...                             "encryption_level": 5,
...                             "algorithms": ["AES-256", "SHA-512"],
...                         }
...                     },
...                 },
...                 {
...                     "id": "B2",
...                     "status": "passive",
...                     "settings": {
...                         "security": {
...                             "encryption_level": 0,
...                             "algorithms": ["AES-256"],
...                         }
...                     },
...                 },
...             ]
...         },
...     },
...     UnravelOptions(marker="Expanded {len} results to {sheet}"),
... )
>>> tables
{
    "*": pd.DataFrame([...]),
    "*.config.modules": pd.DataFrame([...]),
    "*.config.modules.*.settings.security.algorithms": pd.DataFrame([...])
}

Where the tables in the example above are as follows.

*:

project

version

config.modules

_id

Alpha-Prime

1.0.3

Expanded 2 results to *.config.modules

0

*.config.modules:

id

status

settings.security.encryption_level

settings.security.algorithms

_id

A1

active

5

Expanded 2 results to *.config.modules.settings.security.algorithms

0.config.modules.0

B2

passive

0

Expanded 1 results to *.config.modules.settings.security.algorithms

0.config.modules.1

*.config.modules.*.settings.security.algorithm:

_value

_id

AES-256

0.config.modules.0.settings.security.algorithms.0

SHA-512

0.config.modules.0.settings.security.algorithms.1

AES-256

0.config.modules.1.settings.security.algorithms.0

These tables can be converted back to the original JSON by applying rel2dict().

>>> from dict2rel import rel2dict
>>> rel2dict(tables)
{
    # original value
}

dict2rel also provides functions for converting JSON into a single table and then back to JSON with flatten() and inflate().