Create a patch from two documents with the JSON patch generator
This JSON patch generator compares two JSON documents and writes the RFC 6902 operations that transform the first into the second. Instead of hand-writing add, remove and replace entries with JSON Pointer paths, you edit a copy of the document and let the tool work out the difference.
The output is a JSON array you can send in an HTTP PATCH request with Content-Type: application/json-patch+json, store as a migration step, or feed to kubectl patch --type json.
Example
Original:
{"id": 7, "name": "Widget", "price": 10, "tags": ["a", "b", "c"], "meta": {"color": "red"}}
Modified:
{"id": 7, "name": "Widget Pro", "price": 10, "tags": ["a", "c"], "meta": {"color": "red", "size": "L"}, "active": true}
Generated patch:
[
{ "op": "replace", "path": "/name", "value": "Widget Pro" },
{ "op": "replace", "path": "/tags/1", "value": "c" },
{ "op": "remove", "path": "/tags/2" },
{ "op": "add", "path": "/meta/size", "value": "L" },
{ "op": "add", "path": "/active", "value": true }
]
Unchanged values (id, price, meta.color) produce no operations. If the two documents are equal, the output is an empty array [] and the status says the documents are identical.
How the diff is computed
- Objects are compared key by key. Keys missing from the modified document become
remove, new keys becomeadd, and shared keys are compared recursively. - Key order is ignored.
{"a": 1, "b": 2}and{"b": 2, "a": 1}are treated as equal. - Arrays are compared position by position. Changed positions become
replace, extra items at the end are removed from the last index backward, and new items are appended with/-. - Type changes, such as a string becoming an object, produce a single
replaceof the whole value.
Because arrays are aligned by index, removing the second of three items shows up as "replace index 1, remove index 2" rather than "remove index 1". Both are correct and give the same result; the positional form is simply more predictable.
The test guard option
With Add "test" guards before changes ticked, every replace and remove is preceded by a test that checks the old value:
[
{ "op": "test", "path": "/price", "value": 10 },
{ "op": "replace", "path": "/price", "value": 12 }
]
If someone else changed price before your patch arrives, the test fails and a compliant server rejects the whole patch. This is a lightweight form of optimistic concurrency control when you cannot use ETags.
Tips
- Check a generated patch by applying it to the original in the JSON Patch tester; the result should equal your modified document.
- If you only need a visual comparison, JSON diff shows changes side by side.
- If the receiving API expects a partial document rather than operations, create an RFC 7396 patch with JSON Merge Patch instead.
Privacy
The comparison runs locally in your browser, so both documents stay on your machine.