Skip to main content

Transform JSON into another JSON using schema

Project description

json-dict-transformer

This package helps transform an input JSON (as Python dict) into another JSON (also dict) using provided schema.

The main goal behind this package was to enable proxying requests between two systems. The first returns JSON data, which can be transformed and sent to the second system.

Transforming is understood as changing the structure of JSON data. For example, splitting some fields or concatenating fields into other fields. In other words, it is a process of reformatting JSON data preserving values but changing the structure.

Example

Take a look at examples/example-1.py

Output:

{
  'full name': 'John Doe',
  'age': '30',
  'address': 'Main Street,123,New York,USA',
  'hobbies': 'MY HOBBIES ARE: football,basketball,tennis'
}

Installation

Install the package using PIP:

pip install json_dict_transformer
# or
pip install git+https://github.com/thevops/json-dict-transformer.git@master

Usage

Input data format

The package needs data in Python dict format. You can convert JSON into dict using the following code:

import json

with open('data.json') as json_file:
    data = json.load(json_file)

print(type(data)) # it should return type `dict`

Selectors

The schema for mapping uses selectors to point to specific fields. For now, there are only 2 available selectors.

json::<field path>

The selector extracts data from a field. A path is built using dot notation. Please, take a look at the above example.

txt::<string>

The selector adds a string. It can be used to add a splitter or any other text to the output data.

<function> (Callable object)

Allows to use a function over the data on the left side of that function. A function can be passed from outside. Requires to handle a single argument of type string.

Similar projects

Changelog

1.0.0

  • The first version

1.1.0

  • Add support for functions for processing data during transformation

1.1.1

  • Fix functions feature

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

json_dict_transformer-1.1.1.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

json_dict_transformer-1.1.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file json_dict_transformer-1.1.1.tar.gz.

File metadata

  • Download URL: json_dict_transformer-1.1.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for json_dict_transformer-1.1.1.tar.gz
Algorithm Hash digest
SHA256 b6efd2350fd1186bfa32dc74f8880c9256366a8366af54f1c63d0c643c5dc8a9
MD5 16310a5b1e1582501bf2942282b3b217
BLAKE2b-256 91ca467a2cd9ac6bd38635ddf9eb057bf4e7a1ac894d867ed0f2bca3fabbe796

See more details on using hashes here.

File details

Details for the file json_dict_transformer-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for json_dict_transformer-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 463eae3ee9ab55b57ee9fcccbfeefaccc0309e449f27e1a9948d6c01402b6822
MD5 fd9baaf421e23fa77e972432b635f822
BLAKE2b-256 82cce96359015e306e05b6bb93090dbc3039ead7ca26ffcb5e27d2d82d26f198

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