Skip to main content

Diff JSON and JSON-like structures in Python

Project description

jsondiff

Diff JSON and JSON-like structures in Python.

Installation

pip install jsondiff

Quickstart

>>> import jsondiff as jd
>>> from jsondiff import diff

>>> diff({'a': 1, 'b': 2}, {'b': 3, 'c': 4})
{'c': 4, 'b': 3, delete: ['a']}

>>> diff(['a', 'b', 'c'], ['a', 'b', 'c', 'd'])
{insert: [(3, 'd')]}

>>> diff(['a', 'b', 'c'], ['a', 'c'])
{delete: [1]}

# Typical diff looks like what you'd expect...
>>> diff({'a': [0, {'b': 4}, 1]}, {'a': [0, {'b': 5}, 1]})
{'a': {1: {'b': 5}}}

# ...but similarity is taken into account
>>> diff({'a': [0, {'b': 4}, 1]}, {'a': [0, {'c': 5}, 1]})
{'a': {insert: [(1, {'c': 5})], delete: [1]}}

# Support for various diff syntaxes
>>> diff({'a': 1, 'b': 2}, {'b': 3, 'c': 4}, syntax='explicit')
{insert: {'c': 4}, update: {'b': 3}, delete: ['a']}

>>> diff({'a': 1, 'b': 2}, {'b': 3, 'c': 4}, syntax='symmetric')
{insert: {'c': 4}, 'b': [2, 3], delete: {'a': 1}}

>>> diff({'list': [1, 2, 3], "poplist": [1, 2, 3]}, {'list': [1, 3]}, syntax="rightonly")
{"list": [1, 3], delete: ["poplist"]}

# Special handling of sets
>>> diff({'a', 'b', 'c'}, {'a', 'c', 'd'})
{discard: set(['b']), add: set(['d'])}

# Load and dump JSON
>>> print diff('["a", "b", "c"]', '["a", "c", "d"]', load=True, dump=True)
{"$delete": [1], "$insert": [[2, "d"]]}

# NOTE: Default keys in the result are objects, not strings!
>>> d = diff({'a': 1, 'delete': 2}, {'b': 3, 'delete': 4})
>>> d
{'delete': 4, 'b': 3, delete: ['a']}
>>> d[jd.delete]
['a']
>>> d['delete']
4
# Alternatively, you can use marshal=True to get back strings with a leading $
>>> diff({'a': 1, 'delete': 2}, {'b': 3, 'delete': 4}, marshal=True)
{'delete': 4, 'b': 3, '$delete': ['a']}

Command Line Client

Usage:

jdiff [-h] [-p] [-s {compact,symmetric,explicit}] [-i INDENT] [-f {json,yaml}] first second

positional arguments:
  first
  second

optional arguments:
  -h, --help            show this help message and exit
  -p, --patch
  -s {compact,symmetric,explicit}, --syntax {compact,symmetric,explicit}
                        Diff syntax controls how differences are rendered (default: compact)
  -i INDENT, --indent INDENT
                        Number of spaces to indent. None is compact, no indentation. (default: None)
  -f {json,yaml}, --format {json,yaml}
                        Specify file format for input and dump (default: json)

Examples:

$ jdiff a.json b.json -i 2

$ jdiff a.json b.json -i 2 -s symmetric

$ jdiff a.yaml b.yaml -f yaml -s symmetric

Development

Install development dependencies and test locally with

pip install -r requirements-dev.txt
# ... do your work ... add tests ...
pytest

Installing From Source

To install from source run

pip install .

This will install the library and cli for jsondiff as well as its runtime dependencies.

Testing before release

python -m build
twine check dist/*

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

jsondiff-2.1.1.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

jsondiff-2.1.1-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file jsondiff-2.1.1.tar.gz.

File metadata

  • Download URL: jsondiff-2.1.1.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.4

File hashes

Hashes for jsondiff-2.1.1.tar.gz
Algorithm Hash digest
SHA256 c7dfd4f8c9307500a536e9b93492b2c1ba62dac2b3c5189aa6e37d63b427b4d8
MD5 b4b97fb92e5fdc45c35c68f99e5476c9
BLAKE2b-256 9ec4efc93ce3c13daeeacd39c98f9b9b720bea85b2970282553a2aad07cf5b01

See more details on using hashes here.

File details

Details for the file jsondiff-2.1.1-py3-none-any.whl.

File metadata

  • Download URL: jsondiff-2.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.4

File hashes

Hashes for jsondiff-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ffab5bc00237c2c9f48a4b07fff7bf7df13e4b98f9585bd00b6e6e5f371a98fc
MD5 7f99f7042a57a9f4f7fb6be2024f30c9
BLAKE2b-256 def0a051f7596cecc421dae7b4e8b62f36bcf5079a5af64aaa3b556c87d952c4

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