Skip to main content

Diff JSON and JSON-like structures in Python

Project description

Diff JSON and JSON-like structures in Python

>>> from jsondiff import diff

>>> diff({'a': 1}, {'a': 1, 'b': 2})
{<insert>: {'b': 2}}

>>> diff({'a': 1, 'b': 3}, {'a': 1, 'b': 2})
{<update>: {'b': 2}}

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

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

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

# Similar items get patched
>>> diff(['a', {'x': 3}, 'c'], ['a', {'x': 3, 'y': 4}, 'c'])
{<update>: [(1, {<insert>: {'y': 4}})]}

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

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

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-0.1.0.tar.gz (3.9 kB view hashes)

Uploaded Source

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