Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json-flat-1.0.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

json_flat-1.0.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file json-flat-1.0.0.tar.gz.

File metadata

  • Download URL: json-flat-1.0.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for json-flat-1.0.0.tar.gz
Algorithm Hash digest
SHA256 931d1df0076b84f87f771164bf418a57ce7490bb6c39c6fa6f8155c42961061d
MD5 1c8dd2b9e030562a23e6b428847e0e38
BLAKE2b-256 e0f5bf726e1b84c85f2b0e86a1f95b8fbb3bce59b90f836764c2e7fc3b9dd397

See more details on using hashes here.

File details

Details for the file json_flat-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: json_flat-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for json_flat-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e9d07804ee9b0c8d008bf5ea4d7001a6eff841a979f02355d195aad06f74ac54
MD5 b88216d7225712f1714124047a55ce5f
BLAKE2b-256 152f5ee1b08dcbf680ef50da5b4fa888345ae9d46f9072d8db331186648541e9

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