Skip to main content

flatsplode

Project description

Flatsplode

pypi python pytest coverage maintainability

Flatten/Explode JSON objects.

Installation

pip install flatsplode

Usage

Use the flatsplode() function to recursively flatten and explode complex JSON objects.

Import the flatsplode function:

from flatsplode import flatsplode

Create a sample object to flatsplode:

item = {
    'id': '78e5b18c',
    'keywords': [
        'fizz',
        'buzz'
    ],
    'attrs': [
        {'name': 'color', 'value': 'green'},
        {'name': 'size', 'value': 42},
    ],
    'deep': {
        'nested': {
            'keys': {
                'fizz': 'buzz',
                'jazz': 'fuzz',
            }
        }
    }
}

Calling flatsplode(item) will return a generator. Use list() to expand:

list(flatsplode(item))

[
    {
        'id': '78e5b18c',
        'keywords': 'fizz',
        'attrs.name': 'color',
        'attrs.value': 'green',
        'deep.nested.keys.fizz': 'buzz',
        'deep.nested.keys.jazz': 'fuzz'
    },
    {
        'id': '78e5b18c',
        'keywords': 'fizz',
        'attrs.name': 'size',
        'attrs.value': 42,
        'deep.nested.keys.fizz': 'buzz',
        'deep.nested.keys.jazz': 'fuzz'
    },
    {
        'id': '78e5b18c',
        'keywords': 'buzz',
        'attrs.name': 'color',
        'attrs.value': 'green',
        'deep.nested.keys.fizz': 'buzz',
        'deep.nested.keys.jazz': 'fuzz'
    },
    {
        'id': '78e5b18c',
        'keywords': 'buzz',
        'attrs.name': 'size',
        'attrs.value': 42,
        'deep.nested.keys.fizz': 'buzz',
        'deep.nested.keys.jazz': 'fuzz'
    }
]

You can also provide your own join-character:

list(flatsplode(item, '/'))

[
    {
        'id': '78e5b18c',
        'keywords': 'fizz',
        'attrs/name': 'color',
        'attrs/value': 'green',
        'deep/nested/keys/fizz': 'buzz',
        'deep/nested/keys/jazz': 'fuzz'
    },
    
]

Flatsploding is useful when converting objects to pandas DataFrame matrices:

import pandas
from flatsplode import flatsplode

pandas.DataFrame(flatsplode(item))

Pandas also has a built in normalizer that will flatten (not not explode) your data:

from flatsplode import explode

pandas.json_normalize(explode(item))

Result:

         id attrs.name attrs.value deep.nested.keys.fizz deep.nested.keys.jazz keywords
0  78e5b18c      color       green                  buzz                  fuzz     fizz
1  78e5b18c       size          42                  buzz                  fuzz     fizz
2  78e5b18c      color       green                  buzz                  fuzz     buzz
3  78e5b18c       size          42                  buzz                  fuzz     buzz

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

flatsplode-0.6.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

flatsplode-0.6.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file flatsplode-0.6.0.tar.gz.

File metadata

  • Download URL: flatsplode-0.6.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for flatsplode-0.6.0.tar.gz
Algorithm Hash digest
SHA256 437da2d4f326b026b4e28a2298aba76f442b0228db6e2fca9acff814d03ac3c5
MD5 6fbfdf1993cd0ac9e97f9abfc68a5f2b
BLAKE2b-256 401a5c0aac0efe709e6d15522f18db2dc6ff72b183c4a0f5107d4e506c56094f

See more details on using hashes here.

File details

Details for the file flatsplode-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: flatsplode-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for flatsplode-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c808aa54bc421b467a5722d4f6d145f7c7dbd259ea203ae344c4d350c251264a
MD5 b03f1d9b33482a6b08513e70fe7e8afe
BLAKE2b-256 d16f865939a7c42f5b0fe1478e3189d108ad4791d5275d4e9c9cb814dbeccf7d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page