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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

File details

Details for the file flatten-single-item-arrays-0.1.tar.gz.

File metadata

  • Download URL: flatten-single-item-arrays-0.1.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.1

File hashes

Hashes for flatten-single-item-arrays-0.1.tar.gz
Algorithm Hash digest
SHA256 88f44be0d158dbcb63b978dba84d8845ca03900ab869b44d36bf5884255b8827
MD5 a76b5f0c82841983db341736e34196d1
BLAKE2b-256 315bcb08fccb86a4114485b3940e37466d858e210f2e64be33d8041aabf9efce

See more details on using hashes here.

File details

Details for the file flatten_single_item_arrays-0.1-py3-none-any.whl.

File metadata

  • Download URL: flatten_single_item_arrays-0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.1

File hashes

Hashes for flatten_single_item_arrays-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fab33e3517dc13e4468fca851dbc1f758cd537355c7d99dbd37c376824544499
MD5 75fe89a4752d7bbf7f2982e5de3f46a8
BLAKE2b-256 65f9ffb26b5a348204f5247fe65e8764cda1e578bf72b22edcb9b3e92abfc53d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page