How to use the json diff to patch
- Paste the original JSON in the first box and the changed JSON in the second.
- Read the patch in the result box. Each operation has an op (add, remove or replace), a path and, for add and replace, a value.
- Tick compact output for a one-line patch, then copy it or download it as patch.json.
- To check it, apply the patch to the original with any RFC 6902 library and compare with the changed JSON.
What a JSON Patch is
A JSON Patch (RFC 6902) is a list of small edits that turns one JSON document into another. Each edit names a place with a JSON Pointer, such as /owner/email, and says what to do there. The patch for the sample changes version to 2, replaces the second tag, removes the third tag, replaces the owner email and adds an active flag, 5 operations in all.
Patches are used in APIs that update part of a document without sending the whole thing, and in tools that store a history of changes. A patch is smaller than the document when little changes, and it states exactly which fields moved. Paths escape the characters / and ~ as ~1 and ~0, so a key like a/b becomes a~1b.
How the comparison is done
Objects are compared key by key, so the order of keys does not matter. A key only in the original is removed, a key only in the changed document is added and a key in both is compared again inside. Numbers are compared by value after parsing, so 1.0 and 1 are the same.
Arrays are compared position by position. If the changed array is shorter, the extra items are removed from the end, and if it is longer the new items are added. That is simple and always correct, but if you insert an item at the front of a long array every later position changes, so the patch is longer than the smallest possible one. The patch does not use the move or copy operations. For plain text changes see the Diff Checker, for checking the files use the JSON Formatter and Validator, and the JSONPath Tester helps find values.
Limits and privacy
Large numbers beyond the range of exact whole numbers in JavaScript may lose precision when parsed, so do not use the tool for documents that hold such values. The tool reports a clear message when either input is not valid JSON, with the position the parser gives.
Both documents stay in your browser. They are not uploaded, saved or sent anywhere. If the two documents are the same, the patch is an empty list. Use the JSON Flatten and Unflatten tool when you want to view nested keys as flat paths.
Assumptions and limits
- Both inputs are valid JSON. Duplicate keys in an object are read as the last one, as JSON parsers do.
- Arrays are compared by position and the patch uses only add, remove and replace.
- Keys in a path are escaped with ~0 and ~1 as the standard says.
- The result is a patch for the exact documents given, and it has not been tested against your own system.
Frequently asked questions
What is RFC 6902?
It is the standard that defines JSON Patch, a format for describing changes to a JSON document as a list of operations such as add, remove, replace, move, copy and test. This tool writes the first three.
Why is my patch long when I only added one item to an array?
Arrays are compared by position. An item added at the start shifts every later item, so each position shows a replace. Adding at the end gives just one add.
Does key order matter?
No. Objects are compared by key, so two documents with the same keys and values in a different order produce an empty patch.
Is my JSON sent to a server?
No. Both documents are parsed and compared in your browser, and nothing is saved or uploaded.