https://github.com/simonw/flatten-single-item-arrays
Given a JSON list of objects, flatten any keys which always contain single item arrays to just a single value
https://github.com/simonw/flatten-single-item-arrays
airtable-api json
Last synced: about 1 year ago
JSON representation
Given a JSON list of objects, flatten any keys which always contain single item arrays to just a single value
- Host: GitHub
- URL: https://github.com/simonw/flatten-single-item-arrays
- Owner: simonw
- License: apache-2.0
- Created: 2021-02-25T14:49:14.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-02-25T14:50:42.000Z (over 5 years ago)
- Last Synced: 2024-10-18T07:53:30.992Z (over 1 year ago)
- Topics: airtable-api, json
- Language: Python
- Homepage:
- Size: 7.81 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# flatten-single-item-arrays
[](https://pypi.org/project/flatten-single-item-arrays/)
[](https://github.com/simonw/flatten-single-item-arrays/releases)
[](https://github.com/simonw/flatten-single-item-arrays/actions?query=workflow%3ATest)
[](https://github.com/simonw/flatten-single-item-arrays/blob/master/LICENSE)
Given a JSON list of objects, flatten any keys which always contain single item arrays to just a single value
## Installation
Install this tool using `pip`:
$ pip install flatten-single-item-arrays
## Usage
This will output the rewritten JSON:
$ flatten-single-item-arrays input.json
You can save it to a file like this:
$ flatten-single-item-arrays input.json > output.json
Use `--debug` to see extra debugging information displayed on standard error:
$ flatten-single-item-arrays input.json --debug > output.json
Item count: 2
count_of_single_item_lists
{
"foo": 2
}
count_of_present_keys
{
"foo": 2,
"bar": 2
}
keys_to_reformat:
- foo
## What this does
This tool accepts the path to a JSON file and outputs a modified version of that JSON file where any keys that are *always* single item lists are rewritten to a single value.
For example, the following input:
```json
[
{
"foo": [
"bar"
],
"bar": 5
},
{
"foo": [
"baz"
],
"bar": 6
}
]
```
Will be transformed to this:
```json
[
{
"foo": "bar",
"bar": 5
},
{
"foo": "baz",
"bar": 6
}
]
```
I built this to help work with data from the Airtable API, which often contains this single-item-list pattern.
## Development
To contribute to this tool, first checkout the code. Then create a new virtual environment:
cd flatten-single-item-arrays
python -mvenv venv
source venv/bin/activate
Or if you are using `pipenv`:
pipenv shell
Now install the dependencies and tests:
pip install -e '.[test]'
To run the tests:
pytest