Skip to main content

json-flat

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

Metadata

Release files for json-flat 1.0.0

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

Source distribution (sdist)

Source distribution for json-flat 1.0.0
File Size Uploaded Provenance Actions
json-flat-1.0.0.tar.gz 6.6 kB Without provenance Details for json-flat-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for json-flat 1.0.0
File Interpreter ABI Platform Actions
json_flat-1.0.0-py3-none-any.whl Python 3 none any Details for json_flat-1.0.0-py3-none-any.whl

Total release size: 14.9 kB

Release files / json-flat-1.0.0.tar.gz

Download URL json-flat-1.0.0.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
931d1df0076b84f87f771164bf418a57ce7490bb6c39c6fa6f8155c42961061d
BLAKE2b-256 checksum
How to use checksums
e0f5bf726e1b84c85f2b0e86a1f95b8fbb3bce59b90f836764c2e7fc3b9dd397
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.1

Release files / json_flat-1.0.0-py3-none-any.whl

Download URL json_flat-1.0.0-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e9d07804ee9b0c8d008bf5ea4d7001a6eff841a979f02355d195aad06f74ac54
BLAKE2b-256 checksum
How to use checksums
152f5ee1b08dcbf680ef50da5b4fa888345ae9d46f9072d8db331186648541e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.1

Security for json-flat 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

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