Install & Compatibility
Where this runs
tested against v1.33 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.010s · 17.9MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.8s · import 0.010s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
JsonPatch
✓ from jsonpatch import JsonPatch
JsonPointerException
✓ from jsonpatch import JsonPointerException
Raised when a patch operation encounters a conflict or error.
apply_patch
✓ from jsonpatch import apply_patch
A convenience function to apply a patch to a document.
make_patch
✓ from jsonpatch import make_patch
A convenience function to generate a patch by diffing two documents.
This quickstart demonstrates how to create a `JsonPatch` object from a list of operations and apply it to a document, as well as how to use `make_patch` to generate a patch by comparing two documents. The `apply` method returns a new modified document by default.
import jsonpatch
# Original document
doc = {"baz": "qux", "foo": "bar", "numbers": [1, 3, 4, 8]}
# 1. Define a patch explicitly
patch_ops = [
{"op": "replace", "path": "/baz", "value": "boo"},
{"op": "add", "path": "/hello", "value": ["world"]},
{"op": "remove", "path": "/foo"},
{"op": "replace", "path": "/numbers/0", "value": 0}, # Replace first element
{"op": "add", "path": "/numbers/-", "value": 99} # Add to end of array
]
patch = jsonpatch.JsonPatch(patch_ops)
# Apply the patch (returns a new object by default)
result = patch.apply(doc)
print("1. Applied patch result:", result)
# Expected: {'baz': 'boo', 'numbers': [0, 3, 4, 8, 99], 'hello': ['world']}
# 2. Generate a patch by diffing two objects
old_doc = {"a": 1, "b": 2, "c": {"d": 3}}
new_doc = {"a": 10, "c": {"e": 5}, "f": 6}
diff_patch = jsonpatch.make_patch(old_doc, new_doc)
print("2. Generated diff patch operations:", diff_patch.patch)
# Expected operations will modify old_doc to become new_doc.
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'jsonpatch'
The 'jsonpatch' Python package is not installed in your current environment or is not accessible in your Python path.
fixInstall the library using pip: `pip install jsonpatch`
AttributeError: 'list' object has no attribute 'items'
This error occurs when attempting to call the `.items()` method (which is designed for dictionaries) on a Python list object. This often happens when a JSON document is parsed into a list (e.g., a JSON array at the root) but is then treated as a dictionary, or when iterating over a list of dictionaries without accessing individual dictionary elements first.
fixEnsure the object you are calling `.items()` on is a dictionary. If it's a list of dictionaries, iterate through the list and apply `.items()` to each dictionary: `for item in my_list: for key, value in item.items(): ...`
jsonpatch.JsonPatchException: Patch could not be applied due to conflict situation
This general exception is raised by the `jsonpatch` library when an operation within the patch document cannot be successfully applied to the target JSON document. Common reasons include attempting to add a key that already exists, operating on a non-existent path (e.g., trying to modify a property whose parent object doesn't exist), or inserting a value into an array at an out-of-bounds position.
fixReview the patch operations and the target document. Ensure all paths specified in the patch exist or are valid for the intended operation (e.g., parent objects for 'add' operations must exist). Validate array indices and avoid attempting to add keys that already exist.
jsonpatch.JsonPointerException: Key 'nonexistent_key' not found in document
This specific exception indicates that a JSON Pointer path used in a patch operation attempts to access a key or array index that does not exist in the target document. For instance, an 'add' operation might try to create a property at `/a/b/c` when `/a/b` does not exist.
fixEnsure all intermediate components of a JSON Pointer path exist in the target document before attempting operations like 'remove', 'replace', or 'test'. For 'add' operations, the *parent* path must exist; if intermediate objects need to be created, they must be added incrementally as separate operations.
Upgrade
Version history
1.33latest on PyPI · released Jun 16, 2023
Audit
Dependencies
No dependency data recorded yet.