Xerobit
Data & Format

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.

MA Mian Ali Khalid Updated Nov 2, 2026 9 min read
Free tool
JSON Diff
Compare two JSON objects structurally with field-by-field diff.
Open tool →
On this page ⌄
  1. The six operations
  2. JSON Pointer: escaping and array indexes
  3. Atomicity: what happens when an operation fails
  4. JSON Patch versus JSON Merge Patch (RFC 7396)
  5. FAQ
  6. Tested

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

OpPatchResult
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:

  • add into 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/owner on a document with no meta failed with OPERATION_PATH_CANNOT_ADD in JS and JsonPointerException: member 'meta' not found in Python.
  • remove and replace require the target to exist (4.2, 4.3). Both failed on /nope: OPERATION_PATH_UNRESOLVABLE in JS, JsonPatchConflict: can't remove a non-existent object 'nope' in Python.
  • move is a remove then an add, and from must not be a proper prefix of path (4.4). Moving /author to /author/inner is illegal. Python raised Cannot move values into their own children; fast-json-patch threw a raw TypeError: Cannot set properties of undefined, so do not rely on it to give a clean error here.
  • test equality: numbers compare numerically, so 1.0 matched 1 in both libraries. Objects ignore key order (test /author with {"name":"Ada"} passed), arrays do not (["b","a"] against ["a","b"] failed with TEST_OPERATION_FAILED). Python also rejected true tested against 1.
  • 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}:

PointerRefers toResult of test
/a~1bkey a/bpassed (value 1)
/m~0nkey m~npassed (value 2)
/~01key ~1passed (value 3)
/key "" (empty string)passed (value 4)
/a/bkey b inside afailed: a is not an object
(empty string)the whole documentvalid, 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 callErrorDocument after
fast-json-patch applyPatch(doc, patch) (default, mutates)TEST_OPERATION_FAILEDtitle is "Changed", half applied
fast-json-patch applyPatch(doc, patch, false, false) (no mutate)TEST_OPERATION_FAILEDunchanged
Python apply_patch(doc, patch) (default copies)JsonPatchTestFailedunchanged
Python apply_patch(doc, patch, in_place=True)JsonPatchTestFailedtitle 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 patchResult
{"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)
Bodyarray of operationspartial JSON document
Set a value to nullyes (replace with null)no, null means delete
Change one array itemyes (/tags/1)no, send the whole array
Move / copy / precondition testyesno
Readabilitylongervery short for simple edits
Media typeapplication/json-patch+jsonapplication/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

Written by Mian Ali Khalid. More guides in Data & Format.