Skip to main content

No project description provided

Project description

JsonRefiner

JsonRefiner is a lightweight Python package designed for transforming dictionaries into other dictionaries with converted data types, aggregations, different paths, and rearrangements of certain properties. It allows you to refine and restructure JSON-like data with ease.

Install

pip install -U jsonrefiner

How to

There are three classes that are used for transforming your input dict.

PropertyRefiner

The basic refiner function object

# Changing the name of the user_id field
PropertyRefiner(

    # Create a new property called "id" to your output dict
    name="id",

    # Convert/ensure the data type from the input dict to be `str`
    # (Any function is allowed here)
    dtype=str, 

    # Point to where, in the original dict, the data is.
    # It works like a folder path to a file. This example
    # is for when {"user_id": "..."} is input.
    path=["user_id"]
)

ListRefiner

The refiner object for transforming lists. Let's say an object has a list of address information and you'd like to apply one transformation to all of the items

ListRefiner(

    # The new name in the output dict
    name="phone",

    # The refiner function object to apply on each item in the list
    refiner=PropertyRefiner(
        name="city", 
        dtype=str, 

        # NOTE that this path is referring from the list object,
        # not from the input object itself.
        path=["city"]
    ),

    # The path in the input object to where we fine the list items
    path=["addresses"]
)

DictRefiner

The refiner object for transforming dicts/objects. This is usually the starting point refiner in most cases.

DictRefiner(

    # The name of the new object. The output from running this
    # refiner function object would have one property "user"
    name="user",

    # Sub refiners applied to the input object's content
    refiners=[

        # Any refiner goes here
        PropertyRefiner(...),
        ListRefiner(...),
        DictRefiner(...),
    ]
)

Full Example

from jsonrefiner import DictRefiner, ListRefiner, PropertyRefiner

# We'd like to convert and reassemble some data from 
# this type of structure:
input_data = {
    "user_id": "12345",
    "name": {
        "first": "Jane",
        "last": "Doe"
    },
    "age": "29",
    "email": "janedoe@example.com",
    "address": {
        "street": "123 Maple Street",
        "city": "Springfield",
        "state": "IL",
        "postal_code": "62704"
    },
    "phone_numbers": [
        {
            "number": "123-456-7890"
        }
    ]
}

# We'd use the Refiner classes to construct a transformation
# as a function that can be applied to the input data.
refiner = DictRefiner(
    name="user",
    children=[

        # Changing the name of the user_id field
        PropertyRefiner(
            name="id",
            dtype=str, 
            path=["user_id"]
        ),

        # Converting the age to an integer
        PropertyRefiner(
            name="age",
            dtype=int, 
            path=["age"]
        ),

        # Turning name into a single string
        DictRefiner(
            name="name",
            children=[
                PropertyRefiner(
                    name="first name", 
                    dtype=str, 
                    path=["name", "first"],
                ),
                PropertyRefiner(
                    name="last name", 
                    dtype=str, 
                    path=["name", "last"]
                )
            ],
            agg=lambda x: f"{x['first name']} {x['last name']}"
        ),
        
        # Turning address into a single string
        DictRefiner(
            name="address",
            children=[
                PropertyRefiner(
                    name="street", 
                    dtype=str, 
                    path=["address", "street"]
                ),
                PropertyRefiner(
                    name="city", 
                    dtype=str, 
                    path=["address", "city"]
                ),
                PropertyRefiner(
                    name="state", 
                    dtype=str, 
                    path=["address", "state"]
                ),
                PropertyRefiner(
                    name="postal_code", 
                    dtype=str, 
                    path=["address", "postal_code"]
                )
            ],
            agg=lambda x: f"{x['street']}, {x['city']}, {x['state']} {x['postal_code']}",
        ),
    ],
)

# We can then apply the refiner to the input data
actual_output_object = refiner(input_data)
expected_output_object = {
    "user": {
        "id": "12345", 
        "age": 29, 
        "name": "Jane Doe", 
        "address": "123 Maple Street, Springfield, IL 62704"
    }
}
assert actual_output_object == expected_output_object

Project details


Download files

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

Source Distribution

jsonrefiner-0.1.2.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

jsonrefiner-0.1.2-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file jsonrefiner-0.1.2.tar.gz.

File metadata

  • Download URL: jsonrefiner-0.1.2.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.9.18 Darwin/23.1.0

File hashes

Hashes for jsonrefiner-0.1.2.tar.gz
Algorithm Hash digest
SHA256 6ecf5d73e6919b20b459437a7a986805ce69a7486726b111dac0b6ea065b5f7d
MD5 bd2ffa90e5b5f6536506e4dc529874f6
BLAKE2b-256 cb84ab56e7cd5fba298b8b0bad71227bcf3293e4b31d25235071dc2c403806ff

See more details on using hashes here.

File details

Details for the file jsonrefiner-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: jsonrefiner-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.9.18 Darwin/23.1.0

File hashes

Hashes for jsonrefiner-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1c06772b652969008241d6cc763a28aa373deb2ef053db8af81f3f29483b90c0
MD5 89210ceb2d45d3781d36e8decfb20a01
BLAKE2b-256 e1860c1d9f4a2668b12fe51aa49dc6ffc61230cd8c383bfd3a9aa7b8295ff7b5

See more details on using hashes here.

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