Skip to main content

Given a JSON list of objects, flatten any keys which always contain single item arrays to just a single value

Project description

flatten-single-item-arrays

PyPI Changelog Tests 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:

[
    {
        "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.

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

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flatten-single-item-arrays-0.1.tar.gz (3.2 kB view hashes)

Uploaded Source

Built Distribution

flatten_single_item_arrays-0.1-py3-none-any.whl (7.8 kB view hashes)

Uploaded Python 3

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