JSON Patch (RFC 6902): All Six Operations With Tested Examples and Merge Patch Compared
JSON Patch add, remove, replace, move, copy and test with outputs verified in fast-json-patch and Python jsonpatch, pointer escaping, the - index, atomicity and merge patch.
On this page ⌄
A JSON Patch (RFC 6902) is an array of operations applied in order, each pointing at a location with a JSON Pointer (RFC 6901). Every example below was run on 2026-11-02 with fast-json-patch 3.1.1 (Node 25.8.0) and, where noted, Python jsonpatch 1.39. The starting document for most of them:
{"title":"Old","draft":true,"tags":["a","b"],"author":{"name":"Ada"}}
The six operations
| Op | Patch | Result |
|---|---|---|
| add (member) | {"op":"add","path":"/views","value":0} | "views":0 added |
| add (existing member) | {"op":"add","path":"/title","value":"New"} | "title":"New" (replaces, no error) |
| add (array insert) | {"op":"add","path":"/tags/1","value":"x"} | "tags":["a","x","b"] |
| add (append) | {"op":"add","path":"/tags/-","value":"z"} | "tags":["a","b","z"] |
| remove | {"op":"remove","path":"/draft"} | draft gone |
| remove (array item) | {"op":"remove","path":"/tags/0"} | "tags":["b"] |
| replace | {"op":"replace","path":"/title","value":"Replaced"} | "title":"Replaced" |
| move | {"op":"move","from":"/title","path":"/author/title"} | title removed, "author":{"name":"Ada","title":"Old"} |
| copy | {"op":"copy","from":"/author/name","path":"/owner"} | "owner":"Ada" added, author.name kept |
| test | {"op":"test","path":"/title","value":"Old"} | passes, document unchanged |
| whole document | {"op":"replace","path":"","value":{"fresh":true}} | {"fresh":true} |
Rules from the RFC (section numbers in parentheses) that the table shows in action:
addinto an array inserts and shifts later items; into an object it sets the member, replacing an existing one (4.1). The parent must exist:add /meta/owneron a document with nometafailed withOPERATION_PATH_CANNOT_ADDin JS andJsonPointerException: member 'meta' not foundin Python.removeandreplacerequire the target to exist (4.2, 4.3). Both failed on/nope:OPERATION_PATH_UNRESOLVABLEin JS,JsonPatchConflict: can't remove a non-existent object 'nope'in Python.moveis a remove then an add, andfrommust not be a proper prefix ofpath(4.4). Moving/authorto/author/inneris illegal. Python raisedCannot move values into their own children; fast-json-patch threw a rawTypeError: Cannot set properties of undefined, so do not rely on it to give a clean error here.testequality: numbers compare numerically, so1.0matched1in both libraries. Objects ignore key order (test /authorwith{"name":"Ada"}passed), arrays do not (["b","a"]against["a","b"]failed withTEST_OPERATION_FAILED). Python also rejectedtruetested against1.- Members the operation does not define MUST be ignored (4).
JSON Pointer: escaping and array indexes
A pointer is a string of /-separated tokens. Two characters are special inside a token: ~ is written ~0 and / is written ~1 (RFC 6901 section 3). Decoding replaces ~1 first, then ~0, so ~01 means the literal text ~1. Tested against {"a/b":1,"m~n":2,"~1":3,"":4}:
| Pointer | Refers to | Result of test |
|---|---|---|
/a~1b | key a/b | passed (value 1) |
/m~0n | key m~n | passed (value 2) |
/~01 | key ~1 | passed (value 3) |
/ | key "" (empty string) | passed (value 4) |
/a/b | key b inside a | failed: a is not an object |
| (empty string) | the whole document | valid, see the table above |
For arrays, the token must be digits with no leading zeros, or -. Tested as replace paths on tags: /tags/01, /tags/-1 and /tags/1.0 all failed (OPERATION_PATH_UNRESOLVABLE, and Python said '01' is not a valid sequence index). The - token points just past the last element (6901 section 4), so it only makes sense for add: add /tags/- appended, but replace /tags/- failed in both libraries (Python: 'path' with '-' can't be applied to 'replace' operation). An index beyond the end is also an error: add /tags/5 on a two-item array gave OPERATION_VALUE_OUT_OF_BOUNDS.
Atomicity: what happens when an operation fails
The RFC says evaluation SHOULD terminate on the first failing operation and that HTTP PATCH is atomic (RFC 6902 section 5, referencing RFC 5789). Whether your library gives you atomicity is another matter. This patch fails at its second operation:
[{"op":"replace","path":"/title","value":"Changed"},
{"op":"test","path":"/draft","value":false},
{"op":"remove","path":"/tags"}]
| Library call | Error | Document after |
|---|---|---|
fast-json-patch applyPatch(doc, patch) (default, mutates) | TEST_OPERATION_FAILED | title is "Changed", half applied |
fast-json-patch applyPatch(doc, patch, false, false) (no mutate) | TEST_OPERATION_FAILED | unchanged |
Python apply_patch(doc, patch) (default copies) | JsonPatchTestFailed | unchanged |
Python apply_patch(doc, patch, in_place=True) | JsonPatchTestFailed | title is "Changed", half applied |
So test as the first operation is a precondition (it aborts before anything changes), but only a non-mutating call protects you if a later operation fails. On a server, apply the patch to a copy, validate, and then save, or run it inside a transaction.
JSON Patch versus JSON Merge Patch (RFC 7396)
RFC 7396 merge patch sends a partial document instead of an operation list, with the media type application/merge-patch+json. JSON Patch uses application/json-patch+json. Results of the merge algorithm (json-merge-patch 1.0.2) applied to {"title":"Old","tags":["a","b"],"author":{"name":"Ada","email":"a@x"},"note":null}:
| Merge patch | Result |
|---|---|
{"title":"New"} | title replaced, everything else kept |
{"author":{"email":null}} | author.email removed |
{"tags":["c"]} | tags becomes ["c"] (array replaced wholesale) |
{"note":null} | note removed (not set to null) |
{"missing":null} | no change |
["whole"] | the whole result is ["whole"] |
| JSON Patch (6902) | Merge Patch (7396) | |
|---|---|---|
| Body | array of operations | partial JSON document |
Set a value to null | yes (replace with null) | no, null means delete |
| Change one array item | yes (/tags/1) | no, send the whole array |
| Move / copy / precondition test | yes | no |
| Readability | longer | very short for simple edits |
| Media type | application/json-patch+json | application/merge-patch+json |
Choose merge patch if your documents never need explicit null values and you only edit objects. Choose JSON Patch when you need array edits, nulls or preconditions. To see the changes between two versions of a document as a list, compare them with the JSON Diff tool, or generate a patch with fast-json-patch compare or Python make_patch ({"a":[1,2],"b":1} to {"a":[1,2,3],"c":2} produced remove /b, add /c, add /a/2). How these libraries differ on arrays is covered in comparing JSON structurally.
FAQ
What is the content type for JSON Patch? application/json-patch+json (RFC 6902 section 6). Merge patch is application/merge-patch+json.
What is the difference between JSON Patch and merge patch? Operations versus partial document. Merge patch cannot set null (it deletes) and replaces arrays wholesale; JSON Patch can do both.
How do I address an array item? Use its zero-based index with no leading zeros (/tags/1), or - with add to append.
What status code should a failed patch return? The RFC does not say; it points to RFC 5789 section 2.2. That section was not checked.
Tested
2026-11-02: Node 25.8.0, fast-json-patch 3.1.1, json-merge-patch 1.0.2, Python 3.11.15, jsonpatch 1.39. RFC 6902, 6901 and 7396 wording checked against the RFC text. A JSON Patch is atomic only if your library applies it to a copy, as shown above.
Keep reading
Related tools
JSON Diff
Compare two JSON objects structurally with field-by-field diff.
JSON Formatter
Format, validate, and beautify JSON online. 100% client-side, your data never leaves your browser.
HTML Viewer & Formatter
Paste HTML and see it rendered live in a sandboxed preview. Format and beautify messy markup, inline CSS and JS. Desktop, tablet and mobile widths.
JSON Viewer
Explore JSON as a collapsible tree. Paste JSON and navigate nested structures with expand/collapse and JSONPath copy.
Written by Mian Ali Khalid. More guides in Data & Format.