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
from json_dict_transformer import translateDictToDict
input_dict = {
"name": "John",
"surname": "Doe",
"age": 30,
"address": {
"street": "Main Street",
"number": 123,
"city": "New York",
"country": "USA"
},
"hobbies": [
"football",
"basketball",
"tennis"
]
}
schema_dict = {
"full name": ["json::name", "txt:: ", "json::surname"],
"age": ["json::age"],
"address": [
"json::address.street",
"txt::,",
"json::address.number",
"txt::,",
"json::address.city",
"txt::,",
"json::address.country"
],
"hobbies": [
"txt::My hobbies are: ",
"json::hobbies"
]
}
output_dict = translateDictToDict(input_dict, schema_dict)
print(output_dict)
Input dict:
{
'name': 'John',
'surname': 'Doe',
'age': 30,
'address': {
'street': 'Main Street',
'number': 123,
'city': 'New York',
'country': 'USA'
},
'hobbies': [
'football',
'basketball',
'tennis'
]
}
Output dict:
{
'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.
Similar projects
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for json_dict_transformer-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1489fde2cb23dd78c6ac4da83f192fc4ecdd5ae358146508c6bb872a96538b36 |
|
MD5 | d519570531d5ae93a05207280e327d3d |
|
BLAKE2b-256 | a48744312b77cfa704affea8748edecefa43d9db0252189a6984923c80039573 |
Hashes for json_dict_transformer-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2e4d73125f3cabe7737e9256aac915011b1af3c02f070abec6266ea625be409 |
|
MD5 | 6ddee65da2d5f11d4d21353a4a18b943 |
|
BLAKE2b-256 | 870e1ab10e0d0d026bd90f00f22da91b1bbc2d606efa244c2e93822d2a9e53ed |