JSON-delta
JSON-delta is a multi-language software suite for computing deltas between JSON-serialized data structures and applying those deltas as patches. It aims to minimize communications overhead when manipulating data structures between separate programs. The Python implementation, currently at version 2.0.2, is considered the reference implementation, though it has seen limited development activity since its last release in October 2020.
Warnings
- gotcha The `json-delta` project has shown limited activity since its last release in October 2020. This indicates that it may not be actively maintained, which could lead to unaddressed bugs, security vulnerabilities, or lack of support for newer Python versions or features.
- gotcha The diff format generated by `json-delta` is a custom sequence of 'stanzas' (e.g., `[[key_path], update_value]`) and is not directly compatible with standard JSON Patch (RFC 6902). Users expecting an RFC 6902 compliant format will need to implement a conversion layer or choose a different library.
- gotcha When `diff` generates a deletion stanza (i.e., a stanza without an `update` value), the `patch` function cannot reconstruct the original deleted content if it's later needed for 'unpatching' or auditing. This means some diffs are not fully reversible without external knowledge of the deleted data.
- gotcha The `diff` function's `minimal` and `verbose` parameters are primarily for backward compatibility and can be superseded by more specific parameters like `array_align`, `compare_lengths`, and `common_key_threshold`. Using `minimal` in conjunction with these specific parameters might lead to unexpected behavior or warnings if `verbose` is also enabled.
Install
-
pip install json-delta
Imports
- diff
from json_delta import diff
- patch
from json_delta import patch
Quickstart
from json_delta import diff, patch
# Original JSON-like data structure
a = {
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown"
},
"hobbies": ["reading", "hiking"]
}
# Modified JSON-like data structure
b = {
"name": "Jane Doe",
"age": 31,
"address": {
"street": "456 Oak Ave",
"city": "Otherville"
},
"hobbies": ["reading", "coding", "swimming"]
}
# Compute the diff
delta = diff(a, b)
print(f"Computed Delta: {delta}")
# Apply the patch to 'a' to get 'b_reconstructed'
b_reconstructed = patch(a, delta)
print(f"Reconstructed B: {b_reconstructed}")
assert b == b_reconstructed
print("Original 'b' and reconstructed 'b' are equal.")