Skip to main content

Configurable and documentable Json transformation and mapping

Project description

Piri

Configurable Data Mapping for mortals


test codecov Python Version wemake-python-styleguide


Documentation (Stable | Latest) | Source Code | Task Tracker

What is Piri

Piri is a JSON to JSON mapper. That means that we read input JSON and create output JSON. How the output is created is based on instructions from a configuration file. The configuration file governs the the output structure and tells piri where in the input to find data and where to place it in the output. In addition to this Piri supports data transformation with data casting, if conditions, combination of data from multiple places and of course setting default values.

This enables you to change any input into the output you desire.

Typical usecases

  • You GET data from api, but need to transform it for your backend system
  • POSTing data to an api that needs data on a different format than what your system produces
  • All your backends speak different language? pipe it through Piri
  • Customer delivers weirdly formatted data? Use Piri to make it sexy
  • Have CSV but need nicely structured JSON? make CSV into a JSON list and transform it with Piri
  • Have XML but need to change it? make it into JSON, transform it with Piri and then dump it to XML again.
  • Customers legacy system needs CSV. Use Piri to transform your nicely structured JSON data into a JSON List that can be easily dumped to CSV

Official Open Piri Solutions

piri-cli, commandline interface for file to file mapping.

piri-web, One Click deploy Web REST API for Piri JSON mapping.

Enterprise solutions

Coming...

Goal

The goal of this library is to make JSON to JSON transformation/mapping easy, configurable and documentable. We achieve this by using a simple but feature-rich JSON configuration which then also acts as documentation and as a contract between parties.

Why

Piri was born because we really dislike mapping. Documenting whatever decisions made in your code so that some product owner understands it is also no me gusto. Transforming data from one format to another is something software engineers do allmost daily... It should be easy! And documenting it shouldn't be something you have to worry about.

After the Worst POC in History we never wanted to do mapping by scripts and code again. This lead to the idea that it should be possible to create a file which governs how the structure should look and how the data should be transformed. This would then be the single source of truth and with Piri we have achieved this.

We believe that this will make collaboration between teams faster and easier. Use Piri to agree with data formats between Front-end and Back-end. Between the 3rd party system and your back-end. You can even use Piri for testing existing integrations ;-)

Features

  • Mapping with configuration File.
  • JSON Schema validation of the config file.
  • Structurally Transform JSON
  • Combine multiple values to one.
  • Default values
  • If statements
    • is, contains, not
  • casting
    • integer, decimal, iso date

Contributing

Please see contribute

Installation

Package is on pypi. Use pip or poetry to install

pip install piri
poetry add piri

Introduction

Have a look at our introduction course here

Quickstart

import simplejson

from piri3.process import process

my_config = {
    'name': 'schema',
    'array': False,
    'objects': [
        {
            'name': 'invoices',
            'array': True,
            'iterables': [
                {
                    'alias': 'invoices',
                    'path': ['root', 'invoices'],
                },
            ],
            'attributes': [
                {
                    'name': 'amount',
                    'mappings': [
                        {
                            'path': ['invoices', 'amount'],
                        },
                    ],
                    'casting': {
                        'to': 'decimal',
                        'original_format': 'integer_containing_decimals',
                    },
                    'default': 0,
                },
                {
                    'name': 'debtor',
                    'mappings': [
                        {
                            'path': ['root', 'customer', 'first_name'],
                        },
                        {
                            'path': ['root', 'customer', 'last_name'],
                        },
                    ],
                    'separator': ' ',
                },
            ],
            'objects': [],
        },
    ],
}

example_data = {
    'root': {
        'customer': {
            'first_name': 'John',
            'last_name': 'Smith',
        },
        'invoices': [
            {
                'amount': 10050,
            },
            {
                'amount': 20050,
            },
            {
                'amount': -15005,
            },
        ],
    },
}

mapped_data = process(example_data, my_config)

with open('resultfile.json', 'w') as output_file:
    output_file.write(simplejson.dumps(mapped_data))

contents of resultfile.json

{
    "invoices": [
        {
            "amount": 100.5,
            "debtor": "John Smith"
        },
        {
            "amount": 200.5,
            "debtor": "John Smith"
        },
        {
            "amount": -150.05,
            "debtor": "John Smith"
        }
    ]
}

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

piri3-3.0.2.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

piri3-3.0.2-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file piri3-3.0.2.tar.gz.

File metadata

  • Download URL: piri3-3.0.2.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for piri3-3.0.2.tar.gz
Algorithm Hash digest
SHA256 59dbd5883aa495efcb0c268d8120e007de5b0eb9e083293da124af8dd3c9ee75
MD5 cc1658b0ae309a4fbd12a7dce5e7a307
BLAKE2b-256 c21afadd65cedb3de20fefd0ca3c462ff099f6e4c20525b5521add5a9d82be5e

See more details on using hashes here.

File details

Details for the file piri3-3.0.2-py3-none-any.whl.

File metadata

  • Download URL: piri3-3.0.2-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for piri3-3.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 edcd123eee21e346df31f3068ec8b225d6064f6668be20f165f567290e9b9291
MD5 2bea73911c171417699de1e3efb05506
BLAKE2b-256 ccd61208408bb5c83939d87588c036ff116f4af54f5d5ce9d50c2d8218a3ef82

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