Skip to main content

Diff JSON and JSON-like structures in Python

Project description

ujsondiff

========

Diff JSON and JSON-like structures in Python.

Installation


pip install ujsondiff

Quickstart


>>> import ujsondiff as jd
>>> from ujsondiff 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}}

# 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']}

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

ujsondiff-3.0.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

ujsondiff-3.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file ujsondiff-3.0.1.tar.gz.

File metadata

  • Download URL: ujsondiff-3.0.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.8.15 Darwin/21.6.0

File hashes

Hashes for ujsondiff-3.0.1.tar.gz
Algorithm Hash digest
SHA256 3151951d23f03adb56cc3b18ad913770445f8339440f0d5e0829b3a5ebcdd0a5
MD5 3821cc48dee3360a5d33c438ae684d07
BLAKE2b-256 abb301aaad0fb76246a89c725bc04afe51316adce7003d66d65f4b129a1f8134

See more details on using hashes here.

File details

Details for the file ujsondiff-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: ujsondiff-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.8.15 Darwin/21.6.0

File hashes

Hashes for ujsondiff-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 531e15353afb2b7e93aff647bc0192db2a0bae885acdbcd5a5a790924c088dda
MD5 f4726808b340bc4da5ebe772054376d5
BLAKE2b-256 0eb85d87d77420d6a65d0bcc3c19b84368e163dce9098f88fb07284e0150e381

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