JSON Patch: a diff you can apply
JSON Patch (RFC 6902) describes how to turn one JSON document into another as a list of small operations, each pointing at a place with a JSON Pointer (RFC 6901) such as /owner/email. Instead of sending the whole document you send the patch, which is what HTTP PATCH APIs, Kubernetes and many sync protocols do. This page opens the tool in the generate mode: paste the document before and the document after and it writes the patch.
A real diff
Before:
{
"name": "stdlib",
"version": "1.0.0",
"tags": ["json", "tools"],
"owner": { "id": 7, "email": "ana@example.com" },
"draft": true
}After:
{
"name": "stdlib",
"version": "1.1.0",
"tags": ["json", "tools", "patch"],
"owner": { "id": 7 },
"license": "GPL-3.0"
}The generated patch:
[
{
"op": "replace",
"path": "/version",
"value": "1.1.0"
},
{
"op": "add",
"path": "/tags/2",
"value": "patch"
},
{
"op": "remove",
"path": "/owner/email"
},
{
"op": "remove",
"path": "/draft"
},
{
"op": "add",
"path": "/license",
"value": "GPL-3.0"
}
]Applying the patch to the first document gives the second back (checked by the tool):
{
"name": "stdlib",
"version": "1.1.0",
"tags": [
"json",
"tools",
"patch"
],
"owner": {
"id": 7
},
"license": "GPL-3.0"
}The six operations
| Operation | What it does |
|---|---|
add | puts a value at the path: a new object key, or a position in an array ("/tags/-" appends). |
remove | deletes the value at the path. |
replace | changes the value at a path that already exists. |
move | takes the value at "from" and puts it at "path". |
copy | copies the value at "from" to "path". |
test | checks that the value at the path equals the one given, and stops the whole patch if not. |
The generator writes add, remove and replace. move, copy and test can be written by hand and are all applied by the tool.
A safer patch: test before changing
With the "guard" option the generator puts a test of the old value before each replace and remove, so the patch fails on a document that changed in the meantime instead of overwriting someone else's edit:
[
{
"op": "test",
"path": "/version",
"value": "1.0.0"
},
{
"op": "replace",
"path": "/version",
"value": "1.1.0"
},
{
"op": "add",
"path": "/tags/2",
"value": "patch"
},
{
"op": "test",
"path": "/owner/email",
"value": "ana@example.com"
},
{
"op": "remove",
"path": "/owner/email"
},
{
"op": "test",
"path": "/draft",
"value": true
},
{
"op": "remove",
"path": "/draft"
},
{
"op": "add",
"path": "/license",
"value": "GPL-3.0"
}
]JSON Pointer escaping
A key that contains / or ~ must be escaped inside a pointer: ~ becomes ~0 and / becomes ~1.
| Key | In a pointer |
|---|---|
a/b | /a~1b |
m~n | /m~0n |
a/b~c | /a~1b~0c |
How arrays are compared
Arrays are compared position by position: a changed item becomes a replace, extra items at the end become adds and missing ones become removes from the end. Moving an item to the front is therefore written as changes of the items between, not as one move; if the order is not important to you, sort the lists before comparing.
How to use the generator
- Paste the original document in the first box and the new one in the second.
- Read the patch; turn on the guard option if you want test operations.
- Copy it, or switch the tool to apply mode to try it on a document.
Frequently asked questions
What is the difference between JSON Patch and JSON Merge Patch?
JSON Patch (RFC 6902) is a list of operations and can handle arrays and move values. JSON Merge Patch (RFC 7396) is a partial document where null means delete: simpler, but it cannot express changes inside an array or set a value to null.
Which HTTP header carries a JSON Patch?
Send it with the PATCH method and Content-Type: application/json-patch+json. Merge patches use application/merge-patch+json.
What happens if a test operation fails?
The patch is atomic: the tool reports which operation failed and returns no partial result, as RFC 6902 requires.