velkaro.Runs in your browser

JSON field notes · 05

JSON diff in Python: DeepDiff, jsondiff and jsonpatch compared

Three libraries and one operator answer different questions: are the documents equal, what changed, or how do I turn one into the other. Here is the real output of each on the same two files.

The short answer

The rest of this page runs all four on the same pair of files so you can see the actual output. Download order-a.json and order-b.json to follow along.

Test setup

Python 3.12.16 on an Apple M2 Mac, packages installed from PyPI on 28 October 2026. Downloads are the last-month figures shown by pypistats.org that day.

PackageVersion testedRequiresLicenseDownloads, last month
deepdiff9.1.0Python ≥ 3.10MIT53,044,205
jsondiff2.2.1Python ≥ 3.8MIT6,839,500
jsonpatch1.38Python ≥ 3.10BSD-3-Clause103,604,610

Download counts include installs pulled in as dependencies of other packages, so they measure how widely a package is installed, not how often people call it directly.

import json
from deepdiff import DeepDiff
import jsondiff, jsonpatch

with open("order-a.json") as f:
    a = json.load(f)
with open("order-b.json") as f:
    b = json.load(f)

What really changed between the files:

Three more differences change nothing in the data: the top-level keys id and status swapped places, the two tags swapped places, and item id: 13 moved from the end of items to the start.

Check equality with ==

a == b
# False

json.loads('{"x": 1, "y": 2}') == json.loads('{"y": 2, "x": 1}')
# True: key order does not matter

This is the fastest check (about 0.1 µs per call on the test files) and enough for a test assertion, but it gives no hint where the difference is.

DeepDiff: a readable report

diff = DeepDiff(a, b)
print(diff.pretty())

Item root['items'][2] removed from iterable.
Type of root['total'] changed from int to str and value changed from 67 to "55.50".
Value of root['status'] changed from "pending" to "shipped".
Value of root['updatedAt'] changed from "2026-10-18T09:12:00Z" to "2026-10-19T14:30:00Z".
Value of root['tags'][0] changed from "gift" to "priority".
Value of root['tags'][1] changed from "priority" to "gift".
Value of root['items'][0]['id'] changed from 11 to 13.
Value of root['items'][0]['sku'] changed from "MUG-01" to "CAP-02".
Value of root['items'][0]['qty'] changed from 2 to 1.
Value of root['items'][1]['id'] changed from 12 to 11.
Value of root['items'][1]['sku'] changed from "TEE-M" to "MUG-01".
Value of root['items'][1]['qty'] changed from 1 to 3.

That is 12 entries: by default DeepDiff compares lists by position, so moving item 13 to the front makes every field of the first two items look changed. The type change on total is reported separately from value changes, which is useful when an API starts sending numbers as strings.

Ignore list order and skip noise

diff = DeepDiff(a, b, ignore_order=True,
                exclude_paths=["root['updatedAt']"])
print(diff.pretty())

Item root['items'][1] removed from iterable.
Type of root['total'] changed from int to str and value changed from 67 to "55.50".
Value of root['status'] changed from "pending" to "shipped".
Value of root['items'][0]['qty'] changed from 2 to 3.

Four entries: exactly the business changes. ignore_order=True pairs the moved items, and exclude_paths drops the timestamp. Note the index in root['items'][1] refers to the first document.

Pair list items by id

DeepDiff(a["items"], b["items"], group_by="id").pretty()

Item root[12] removed from dictionary.
Value of root[11]['qty'] changed from 2 to 3.

group_by turns a list of dicts into a dict keyed by that field, so the report names items by id instead of position. Use diff.to_json() when you need the report as JSON.

jsondiff: a compact change set

jsondiff.diff(a, b)

{'status': 'shipped', 'updatedAt': '2026-10-19T14:30:00Z',
 'tags': {insert: [(1, 'gift')], delete: [0]},
 'items': {0: {'id': 13, 'sku': 'CAP-02'},
           1: {'id': 11, 'sku': 'MUG-01', 'qty': 3}, delete: [0]},
 'total': '55.50'}

The default “compact” syntax lists only the new values, which makes it small but hard to review: the items entry rewrites two positions and deletes one rather than saying that item 12 left. syntax="symmetric" adds the old values. Two things we hit in practice:

jsonpatch: an RFC 6902 patch

patch = jsonpatch.make_patch(a, b)
list(patch)

[{"op": "replace", "path": "/status", "value": "shipped"},
 {"op": "replace", "path": "/updatedAt", "value": "2026-10-19T14:30:00Z"},
 {"op": "move", "from": "/tags/1", "path": "/tags/0"},
 {"op": "remove", "path": "/items/0"},
 {"op": "remove", "path": "/items/0"},
 {"op": "add", "path": "/items/1", "value": {"id": 11, "sku": "MUG-01", "qty": 3}},
 {"op": "replace", "path": "/total", "value": "55.50"}]

patch.apply(a) == b
# True

Seven operations that turn A into B, verified by applying them. A patch describes how to edit A, not what changed: item 11 is removed and added again instead of reported as a quantity change. Use it to send or store changes; use DeepDiff to explain them. See how JSON Patch operations work for the rules on array indexes.

Side by side

a == bDeepDiffjsondiffjsonpatch
OutputTrue / FalseReport by path, as text, dict or JSONDict of new valuesRFC 6902 operations
Result on the test filesFalse12 entries; 4 with ignore_order + exclude_paths5 top-level keys changed7 operations
Ignore list orderNoYes, ignore_orderNoNo
Skip a key or pathNoYes, exclude_paths, exclude_regex_pathsNot in 2.2.1No
Pair list items by a fieldNoYes, group_byNoNo
Time per call on the test files0.1 µs458 µs; 707 µs with ignore_order20 µs95 µs

Timings are the fastest of five runs of 2,000 calls each with timeit on the M2 Mac above. They are for two small documents and only show relative cost; DeepDiff does more work per call because it builds a full report.

Check the result without code

The same pair in the Velkaro JSON diff gives 12 changes by position, and 4 with “Match array items by: id”, “Ignore array order” and “Ignore keys: updatedAt” — the same four changes as DeepDiff with ignore_order and exclude_paths. It runs in the browser, so you can paste production data without sending it anywhere, and download the JSON Patch or the report.

Compare two JSON files online

Questions

How do I compare two JSON files in Python?

Load both with json.load and compare with ==. For a list of differences use DeepDiff(a, b); for an RFC 6902 patch use jsonpatch.make_patch(a, b).

Does key order matter when comparing JSON in Python?

No. json.load returns dictionaries, and dictionaries compare equal when they have the same keys and values in any order. List order does matter unless you use DeepDiff with ignore_order=True.

What is the difference between DeepDiff and jsondiff?

DeepDiff reports what changed at each path, can ignore list order, skip paths and pair items by a field. jsondiff returns a compact structure of the new values and was about 23 times faster on the test files (20 µs against 458 µs per call), but in version 2.2.1 has no option to ignore order or skip keys.

How do I ignore a field like a timestamp in DeepDiff?

Pass exclude_paths=["root['updatedAt']"], or exclude_regex_paths to match the key at any depth.

Can I turn a Python JSON diff into a JSON Patch?

Yes. jsonpatch.make_patch(a, b) returns a JsonPatch object; list(patch) gives the operations and patch.apply(a) reproduces b.

Related