Given a JSON list of objects, flatten any keys which always contain single item arrays to just a single value
Install this tool using pip
:
$ pip install flatten-single-item-arrays
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
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:
[
{
"foo": [
"bar"
],
"bar": 5
},
{
"foo": [
"baz"
],
"bar": 6
}
]
Will be transformed to this:
[
{
"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.
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