A Python library to flatten a nested json
Project description
A Python library to flatten a nested json.
Use cases
To get a csv file [1] out of the json document stores like elasticsearch, mongodb, bigquery etc [2]. All of these databases allow their documents to be nested. Out there are plenty of libraries which can convert plain json (not nested) to csv but really suck when the json is nested. This is the real use case we wanted to solve and this library came out as a result.
Installation
pip install json-flat
Quick Start
from jsonflat import JsonFlat
# Load the input json into python data structure.
# An input list of rows (nested or plain).
rows = [
{'a': 1, 'b': {}, 'c': {'d': 73}},
{'a': 'hello', 'b': ['thank', 'you']}
]
print(JsonFlat().flatten(rows))
{
'field_names': ['a', 'b', 'c.d'],
'rows': [
{'a': 1, 'b': None, 'c.d': 73},
{'a': 'hello', 'b': 'thank'},
{'a': 'hello', 'b': 'you'}
]
}
field_names is an array of all the fields (attributes) encountered in the input json. The order of the fields will be based on their appearance in input json. A field appearing first in the input json will be the first in the field_names, second will be the second and so on.
rows is an array containing the actual flattened rows. This is guaranteed to be always an array of objects irrespective of what the input looks like. No guarantee that the order of keys within each object is the same as that of the input (use field_names for ordering related stuff). Also no guarantee that each of these objects contain all the keys appearing in the field_names (use null as the value for the non-existing keys) but guaranteed to contain all the keys appearing in the input json corresponding to this object.
Documentation
Read the documentation.
Specification (what in input becomes what in output?)
Read the specification.
Footnotes
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
Hashes for json_flat-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9d07804ee9b0c8d008bf5ea4d7001a6eff841a979f02355d195aad06f74ac54 |
|
MD5 | b88216d7225712f1714124047a55ce5f |
|
BLAKE2b-256 | 152f5ee1b08dcbf680ef50da5b4fa888345ae9d46f9072d8db331186648541e9 |