Flatsplode
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 (but 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
Metadata
Release files for flatsplode 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| flatsplode-1.0.0.tar.gz | 7.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| flatsplode-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.0 kB
Release files / flatsplode-1.0.0.tar.gz
| Download URL | flatsplode-1.0.0.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8a99f269686bdc59abd5f476670cdfb83e734a0a153fc31f88d1ad9fabca8f0d
|
|
BLAKE2b-256 checksum How to use checksums |
71669cf733e062b7583c22fccc758599d75889c5a1c46f4bb82d3be80674e4a8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.3
|
Release files / flatsplode-1.0.0-py3-none-any.whl
| Download URL | flatsplode-1.0.0-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
90bd95ec30fbd7663ca1ccaf517499fe36b304eac938d670d98467e2da1b75a1
|
|
BLAKE2b-256 checksum How to use checksums |
f1701dd0207e3fe24c2cbbdd52851d2e890fcc4849292a8c0106123ed4c6b729
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.3
|