Skip to main content

flatjsondict: efficient JSON-like data transformation tool

What is it?

flatjsondict is nested JSON-like object transformation tool that provides FlatJson object for flat Pandas Series index-like label and filesystem path-like label access and manipulation for nested JSON-like data. Primarily used to efficently transform Pandas Series with MultiIndex index to nested JSON-like (dict, list) object and nested JSON-like data to flat Pandas Series with MultiIndex index.

Labels need to be tuples or path-like strings. The default separator for path-like text labels is /, but can be configured by constructor or updated by calling :meth:FlatJson.set_keypath_separator.

Note that FlatJson provides :meth:FlatJson.to_series() to prepare JSON-like data for efficiently creating Pandas Series object with data MultiIndex index allowing to efficiently transform nested JSON-like object to Pandas Series.

Note that FlatJson provides :meth:FlatJson.to_json() to efficiently create nested JSON-like object from flat tuple-like label dictionary. Alternatively, FlatJson can be used as the target dictionary-like class when calling Series.to_dict(FlatJson), then FlatJson.to_json() can be called to return nested json-like data for use with JSON:API applications.

Examples

Constructing FlatJson from a nested dictionary.

>>> import flatjsondict as fj
>>> d = {'a': 1, 'b': {'c': 3}}
>>> d_fj = fj.FlatJson(data=d)
>>> d_fj.to_dict()
{('a',): 1, ('b', 'c'): 3}

Note that the nested objects are dictionaries hence all label keys are string values.

>>> d = {'a': 1, 'b': ['c', 3]}
>>> d_fj = fj.FlatJson(data=d)
>>> d_fj.to_dict()
{('a',): 1, ('b', 0): 'c', ('b', 1): 3}

Note that the labels keys for nested lists are integer values.

>>> d = {'a': 1, 'b': ['c', 3]}
>>> d_fj = fj.FlatJson(data=d)
>>> d_fj.to_series()
{('a', ''): 1, ('b', 0): 'c', ('b', 1): 3}

Note that for nested object with varying nesting depth the label tuple length is normalized (padded) when calling :meth:FlatJson.to_series(). Such label length normalization prepares FlatJson data for efficient creation of Pandas Series objects with MultiIndex index allowing to transform deeply nested JSON object data to Pandas Series.

Transforming Pandas Series and DataFrame to and from nested JSON-like data.

>>> import pandas as pd
>>> import flatjsondict as fj
>>> d = {'a': 1, 'b': ['c', 3]}
>>> ds = FlatJson(d).to_series(into = pd.Series)
>>> ds
a       1
b  0    c
    1    3

>>> ds.to_dict(into = fj.FlatJson)
{('a',): 1, ('b', 0): 'c', ('b', 1): 3}

>>> ds.to_dict(into = fj.FlatJson).to_json()
{'a': 1, 'b': ['c', 3]}

Note that you can pass FlatJson to Pandas Series.to_dict(into = FlatJson) to directly derive FlatJson from Pandas Series data. Then use FlatJson.to_json() to return nested JSON-like data.

>>> ds.unstack()
    0    1     
a  NaN  NaN    1
b    c    3  NaN

>>> ds.unstack().stack()
a       1
b  0    c
    1    3

Note that (un)stacking Pandas Series with .unstack() and .stack() allows to tranform the nested JSON-like data to and from convenient tabular-like Pandas DataFrame data structure. Note that (un)stacking by default sorts the level(s) in the resulting Pandas DataFrame MultiIndex columns and therefore can alter the order or elements.

Constructing nested json-like data from FlatJson-like dictioaries.

>>> import flatjsondict as fj
>>> d = {('a', ''): 1, ('b', 0): 'c', ('b', 1): 3}
>>> d_fj = fj.FlatJson(data=d)
>>> d_fj.to_dict()
{('a',): 1, ('b', 0): 'c', ('b', 1): 3}
>>> d_fj.to_json()
{'a': 1, 'b': ['c', 3]}

>>> d_fj.paths()
['a', 'b/0', 'b/1']
>>> d_fj.to_dict(join_key_tuples = True)
{'a': 1, 'b/0': 'c', 'b/1': 3}

Note that you can pass FlatJson to Pandas Series.to_dict(FlatJson) to directly derive FlatJson from Pandas Series data. Then use FlatJson.to_json() to return nested JSON-like data.

Slicing FlatJson using multiple keys.

>>> import flatjsondict as fj
>>> d = {'a': 1, 'b': ['c', 3]}
>>> d_fj = fj.FlatJson(data=d)
>>> d_fj.to_dict()
{('a',): 1, ('b', 0): 'c', ('b', 1): 3}
>>> d_fj.slice(('a',), ('b', 1)).to_json()
{'a': 1, 'b': [3]}

Metadata

Release files for flatjsondict 1.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flatjsondict 1.2.3
File Size Uploaded
flatjsondict-1.2.3.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flatjsondict 1.2.3
File Interpreter ABI Platform
flatjsondict-1.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 15.1 kB

Release files / flatjsondict-1.2.3.tar.gz

Download URL flatjsondict-1.2.3.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
10ca3e9dcf525cb5459d66c6edf3d29e916f71307798044cc55b0876f889f300
BLAKE2b-256 checksum
How to use checksums
6f42009c6251b4585fbc6d7ee140ae0172de4c7e50197fb62cce9fa9e2dc4609
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.3 CPython/3.13.15 Linux/5.15.154+

Release files / flatjsondict-1.2.3-py3-none-any.whl

Download URL flatjsondict-1.2.3-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
46edfa6d5ad0af9ed0414f0718b1d0e64cc2aba869bb586858eb1749ec8446c2
BLAKE2b-256 checksum
How to use checksums
0657c37a919862e37a2d5c94ff6e858cdd2bf506bcb62221bd3e96f1c4839249
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.3 CPython/3.13.15 Linux/5.15.154+

Release history Release notifications | RSS feed

This release

1.2.3 This release

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page