Skip to main content

JSON-like data manipulation and transformation to and from nested parent-child and flat label-value data items.

Project description

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]}

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

flatjsondict-1.2.2.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

flatjsondict-1.2.2-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file flatjsondict-1.2.2.tar.gz.

File metadata

  • Download URL: flatjsondict-1.2.2.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Linux/5.4.109+

File hashes

Hashes for flatjsondict-1.2.2.tar.gz
Algorithm Hash digest
SHA256 009956f1478a6c28c5932fdeef839ca266693c8f628f0f02543bf1a90c192b06
MD5 dd3b783703668cca5b3c70db06d3f5c4
BLAKE2b-256 d1c3407069cc4eb093174df82eba2e0b4873245505186434a1884c553ef7fe41

See more details on using hashes here.

File details

Details for the file flatjsondict-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: flatjsondict-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Linux/5.4.109+

File hashes

Hashes for flatjsondict-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fd0bf01d2e034f2cf4d301da1c02f0b25e25298bec6eff942eb5114a866a3f32
MD5 44b7ce2cf2d78d9aa216b6a1eb339bdf
BLAKE2b-256 9b4f84b319a12b5ce8f1c60bee572491999ebcab8355f9daaf98be8cd6329877

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