Skip to main content

JSON comparator

Project description

The JSON Comparison package

Build Status License: MIT

This package is designed to compare two objects with a JSON-like structure and data types.

Installation

pip install jsoncomparison

Usage

First you need to define two variables: expected & actual. Think of them as the same variables that you use in unittests.

Expected - the original data object, that you want to see. Actual - the given data object.

Then we will transfer these objects to check and identify the difference between them:

from jsoncomparison import Compare


expected = {
    'project': {
        'name': 'jsoncomparison',
        'version': '0.1',
        'license': 'MIT',
        'language': {
            'name': 'python',
            'versions': [
                3.5,
                3.6
            ]
        }
    },
    'os': 'linux'
}

actual = {
    'project': {
        'name': 'jsoncomparison',
        'version': 0.1,
        'license': 'Apache 2.0',
        'language': {
            'name': 'python',
            'versions': [
                3.6
            ]
        }
    }
}

diff = Compare().check(expected, actual)
assert diff != {}

The check method returns a dictionary of differences between expected and actual objects, and report about it.

Diff output:

{
    "project": {
        "version": {
            "_message": "Types not equal. Expected: <str>, received: <float>",
            "_expected": "str",
            "_received": "float"
        },
        "license": {
            "_message": "Values not equal. Expected: <MIT>, received: <Apache 2.0>",
            "_expected": "MIT",
            "_received": "Apache 2.0"
        },
        "language": {
            "versions": {
                "_length": {
                    "_message": "Lengths not equal. Expected <2>, received: <1>",
                    "_expected": 2,
                    "_received": 1
                },
                "_content": {
                    "0": {
                        "_message": "Value not found. Expected <3.5>",
                        "_expected": 3.5,
                        "_received": null
                    }
                }
            }
        }
    },
    "os": {
        "_message": "Key does not exists. Expected: <os>",
        "_expected": "os",
        "_received": null
    }
}

To check if the objects are the same, just call:

diff = Compare().check(expected, actual)
self.assertEqual(diff, {})

Configuration

You can define your configuration file and transfer it to Compare class constructor. But first make sure that the structure of your configuration file is similar to this.

Example:

import json
from jsoncomparison import Compare


with open('config', 'r') as fp:
    config = json.load(fp)

cmp = Compare(config)

Output

By default, the configuration file does not allow printing the comparison result to the console, but at the same time writes the results to a file. These settings can be changed in your configuration file:

{    
    "output": {
        "console": true,
        "file": {
            "name": "my-output-file.json",
            "indent": 4
        }
    }
}

Ignore rules

What if you do not want to compare some values and keys of objects from your JSON's? In this case, you can define exception rules and pass them to the class constructor.

Let's go back to the example above:

from jsoncomparison import Compare


expected = {
    # ...
}

actual = {
    # ...
}

rules = {
    'project': {
        'version': '*',
        'license': '*',
        'language': {
            'versions': {
                '_values': [
                    3.5
                ]
            }
        }
    },
    'os': '*',
}

diff = Compare(rules=rules).check(expected, actual)
assert diff == {}

Now that we have added exceptions to the missing values, the comparison test has been successfully passed!

Links

You can see a more complex comparison example that I used to test the correct operation of an application: link.

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

jsoncomparison-0.1.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

jsoncomparison-0.1.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file jsoncomparison-0.1.1.tar.gz.

File metadata

  • Download URL: jsoncomparison-0.1.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

File hashes

Hashes for jsoncomparison-0.1.1.tar.gz
Algorithm Hash digest
SHA256 15c1c8ab69a940a0722bf663e8596dbe52859f9b8ee905930d18020d481531a9
MD5 57743e9b3fd4003d1f9fdc019d7a22bf
BLAKE2b-256 db31542344870d8a49d164fab072618bfb6975f55ab882afe9fb8ddd8979981f

See more details on using hashes here.

File details

Details for the file jsoncomparison-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: jsoncomparison-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

File hashes

Hashes for jsoncomparison-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8c8abbdf0d8cbc4c9ff0864c7975b9cf3d5eb06c650c68109a1ac08ca4443484
MD5 a6cfc8ec0c38253d3ef66cac6db5f73b
BLAKE2b-256 6a2c0e131d06bcafc8e39fa308a1f4522b523ab14ca5fdca399597378c6fa21c

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