Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/amancevice/flatsplode
Flatten/Explode JSON objects
https://github.com/amancevice/flatsplode
json python
Last synced: about 2 months ago
JSON representation
Flatten/Explode JSON objects
- Host: GitHub
- URL: https://github.com/amancevice/flatsplode
- Owner: amancevice
- License: mit
- Created: 2018-11-13T21:01:03.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-06-18T16:29:58.000Z (8 months ago)
- Last Synced: 2024-12-13T15:14:50.389Z (2 months ago)
- Topics: json, python
- Language: Python
- Homepage:
- Size: 146 KB
- Stars: 17
- Watchers: 4
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Flatsplode
[](https://pypi.org/project/flatsplode/)
[](https://pypi.org/project/flatsplode/)
[](https://github.com/amancevice/flatsplode/actions/workflows/pytest.yml)
[](https://codeclimate.com/github/amancevice/flatsplode/test_coverage)
[](https://codeclimate.com/github/amancevice/flatsplode/maintainability)Flatten/Explode JSON objects.
## Installation
```bash
pip install flatsplode
```## Usage
Use the `flatsplode()` function to recursively flatten and explode complex JSON objects.
Import the `flatsplode` function:
```python
from flatsplode import flatsplode
```Create a sample object to flatsplode:
```python
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:
```python
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:
```python
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:
```python
import pandas
from flatsplode import flatsplodepandas.DataFrame(flatsplode(item))
```Pandas also has a built in normalizer that will flatten (but not explode) your data:
```python
from flatsplode import explodepandas.json_normalize(explode(item))
```Result:
```plaintext
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
```