jsondiff
jsondiff is a Python library designed to compare JSON and JSON-like structures, providing a detailed difference report. It is actively maintained, with its latest version being 2.2.1, and has a consistent release cadence with recent updates addressing bug fixes and new features.
Warnings
- breaking The command-line interface (CLI) for `jsondiff` underwent breaking changes. The `jsondiff` command was deprecated in v1.2.0 in favor of `jdiff` and then entirely removed in v2.0.0. Users relying on the old CLI must switch to `jdiff` or implement programmatically.
- breaking Support for Python versions prior to 3.8 was officially dropped in version 2.1.2. Users on Python 3.7 or older will encounter installation or runtime errors.
- gotcha The `diff` function supports different output syntaxes: 'compact' (default), 'symmetric', and 'explicit'. The default 'compact' syntax may not always clearly distinguish between added/removed keys vs. changed values, which can be a source of confusion.
- gotcha Version 2.2.0 introduced the ability to exclude specific paths from the diff calculation using the `exclude_paths` argument. Prior to this, achieving similar functionality would require manual post-processing of the diff result.
- gotcha An issue where `Symbol.__eq__` was not robustly protected by an instance check was fixed in version 2.2.1. This could potentially lead to unexpected behavior or errors when comparing `jsondiff.symbols.xxx` objects with other non-symbol types.
Install
-
pip install jsondiff
Imports
- diff
from jsondiff import diff
- symbols
from jsondiff import symbols
Quickstart
from jsondiff import diff, symbols
json1 = {
"name": "Alice",
"age": 25,
"city": "New York",
"hobbies": ["reading", "hiking", "coding"]
}
json2 = {
"name": "Bob",
"age": 30,
"city": "London",
"occupation": "Engineer",
"hobbies": ["reading", "running", "coding"]
}
# Basic diff (compact syntax by default)
compact_diff = diff(json1, json2)
print("Compact Diff:", compact_diff)
# Explicit diff (more detailed output with symbols)
explicit_diff = diff(json1, json2, syntax='explicit')
print("\nExplicit Diff:", explicit_diff)
# Accessing specific changes using symbols
if symbols.insert in explicit_diff:
print(" Inserted keys:", explicit_diff[symbols.insert])
if symbols.delete in explicit_diff:
print(" Deleted keys:", explicit_diff[symbols.delete])
if symbols.update in explicit_diff:
print(" Updated keys:", explicit_diff[symbols.update])