Skip to main content
CuteJSON
↑↓ Navigate Enter Select Esc Close

JSON Patch vs JSON Merge Patch: which one to send

Published 12 min read By Akash Kinkar Pandey

An API that accepts partial updates through HTTP PATCH has to agree with its clients on what the request body means. Two IETF standards answer that question for JSON, and they answer it very differently. JSON Patch, RFC 6902, sends a list of operations. JSON Merge Patch, RFC 7396, sends a partial copy of the document and lets the server work out the changes.

The short answer: use Merge Patch when your documents are mostly objects, never need a value set to null, and never need part of an array changed. Use JSON Patch for everything else, and whenever an update must refuse to run against a document that has changed since the client last read it.

Key Takeaways

  • JSON Patch is an ordered array of add, remove, replace, move, copy and test operations, each aimed at one location by a JSON Pointer.
  • JSON Merge Patch is a JSON value shaped like the target. Keys present are set, keys set to null are deleted, keys absent are left alone.
  • Merge Patch cannot set a value to null and cannot change one element of an array. Both limits are stated in RFC 7396 itself.
  • A JSON Patch is all or nothing: if one operation fails, the whole patch is not applied. Its test operation turns that into a safety check.
  • The two use different media types, application/json-patch+json and application/merge-patch+json, so a server can accept both.

The same change, written both ways

The quickest way to see the difference is one edit expressed in each format (the JSON Patch tool can write the operation list for your own documents). Start with this document:

{
  "id": 1042,
  "title": "Sprint notes",
  "owner": { "name": "Ada", "email": "ada@example.com" },
  "tags": ["draft", "internal", "q3"],
  "archivedAt": "2026-01-14"
}

The change: rename the title, delete the owner’s email, drop the internal tag, and set archivedAt to null while keeping the field.

As a JSON Merge Patch:

{
  "title": "Sprint 14 notes",
  "owner": { "email": null },
  "tags": ["draft", "q3"]
}

As a JSON Patch:

[
  { "op": "replace", "path": "/title", "value": "Sprint 14 notes" },
  { "op": "remove", "path": "/owner/email" },
  { "op": "remove", "path": "/tags/1" },
  { "op": "replace", "path": "/archivedAt", "value": null }
]

The merge patch is shorter and reads like the data. But notice what it could not say. There is no way to write “set archivedAt to null” in it, because "archivedAt": null means “delete archivedAt”. And to drop one tag it had to resend the whole tags array. The JSON Patch names each change explicitly, so neither problem exists. Most of what follows comes back to that trade: the merge patch is easier to read, the JSON Patch says exactly what it means.

How JSON Merge Patch works

A merge patch describes the result by example. RFC 7396 gives the whole algorithm as about a dozen lines of pseudocode, and it comes down to four rules:

  1. If the patch is an object, walk its keys. A key whose value is null is removed from the target. Any other key is merged into the target’s value at that key, recursively.
  2. If the target is not an object but the patch is, the target is first replaced with an empty object.
  3. If the patch is anything other than an object (an array, a string, a number, true, false or null), it replaces the target outright.
  4. Keys the patch does not mention are left untouched.

Rule 3 is where arrays go wrong. An array in the patch is not an object, so it replaces the array in the target wholesale. RFC 7396 says so directly: “it is not possible to patch part of a target that is not an object, such as to replace just some of the values in an array.” To change the price of one item in a 500 item list, the client sends all 500.

The null rule has a second, less obvious edge. The RFC’s own test cases include applying {"a": {"bb": {"ccc": null}}} to an empty object. The result is {"a": {"bb": {}}}: the containers are created, and the null inside them deletes a key that was never there. Nulls already present in the target are not affected, so {"e": null} patched with {"a": 1} keeps its null.

The specification is candid about who this format is for: documents “that primarily use objects for their structure and do not make use of explicit null values.” If your data fits that description, Merge Patch is pleasant to use, and it maps naturally onto form submissions where only the edited fields are sent.

How JSON Patch works

A JSON Patch is an array of operation objects. Each one has an op, a path written as a JSON Pointer, and, depending on the operation, a value or a from. RFC 6902 defines six:

Operation What it does Fails when
add Inserts into an array, or adds or replaces an object member The parent object or array does not exist
remove Deletes the value at path The target does not exist
replace Swaps the value at path for value The target does not exist
move Removes the value at from and adds it at path from does not exist, or path is inside from
copy Adds a copy of the value at from to path from does not exist
test Checks that the value at path equals value The values differ

Operations run in order, and each one sees the document as the previous one left it. The RFC’s error handling rule is short: if any operation fails, “application of the entire patch document SHALL NOT be deemed successful.” HTTP PATCH itself is atomic under RFC 5789, which says the server “MUST apply the entire set of changes atomically.” So a five operation patch that fails on the fourth leaves the document exactly as it was.

Two details from the spec trip people up. add on an object member that already exists replaces it rather than failing, so add and replace differ only when the member is missing. And in an add, the path /tags/- means “after the last element”, the standard way to append to an array.

Where JSON Patch earns its extra verbosity

Setting a value to null

{ "op": "replace", "path": "/archivedAt", "value": null } sets the field to null and keeps it. { "op": "remove", "path": "/archivedAt" } deletes it. The two intents are spelled differently. If null and “absent” mean different things in your API (and in many they do: “no value yet” versus “not part of this resource”), Merge Patch cannot carry the difference. The JSON data types guide covers why null is a real value and not the same as a missing key.

Changing one element of an array

{ "op": "replace", "path": "/items/37/price", "value": 8.95 } touches one field of one element. With Merge Patch there is no equivalent short of resending the array.

Refusing to overwrite someone else’s change

test has no Merge Patch counterpart, and it is the operation I would miss most. Put it first:

[
  { "op": "test", "path": "/tags/1", "value": "internal" },
  { "op": "remove", "path": "/tags/1" }
]

If another client already removed or reordered the tags, /tags/1 no longer holds "internal", the test fails, and because the patch is atomic nothing is removed. Without the test, the remove would delete whatever happens to sit at index 1 now. RFC 6902’s own example shows this pattern: a replace followed by a failing test results in no change at all. For whole-resource protection, pair patches with an If-Match header carrying the ETag you read.

Mistakes that bite in practice

Array indexes shift as you go. To remove "b" and "c" from ["a", "b", "c", "d"], the patch remove /tags/1 then remove /tags/2 deletes "b" and then "d", because after the first removal "d" sits at index 2. Remove from the highest index down, or test before each removal.

Merge patches are not partial documents with types. Sending {"owner": null} because a form field was blank deletes the entire owner object. Clients that build merge patches from forms need to omit untouched fields, not null them out.

A non-object merge patch replaces everything. Per rule 3, a merge patch of [], "x" or null turns the whole target into that value. An empty body parsed as null by a lenient framework would wipe the resource.

Retries behave differently. Applying the same merge patch twice gives the same result as applying it once, since it only sets and deletes keys. A JSON Patch that appends with /items/- adds a second item on a retry. If a client retries patches after timeouts, either use Merge Patch for that endpoint or guard the JSON Patch with a test.

Formatting and precision are not promised. RFC 7396 states there is “no expectation” that applying a merge patch preserves whitespace, member order or “number precision beyond what is available in the target’s implementation.” The same applies in practice to JSON Patch libraries that parse into native types, so a 20 digit ID can be rounded by a library that reads numbers as doubles.

Telling the server which one you sent

Each format has its own registered media type, and a server should dispatch on Content-Type, not guess from the body:

JSON Patch JSON Merge Patch
Specification RFC 6902, April 2013 RFC 7396, October 2014 (obsoletes RFC 7386)
Media type application/json-patch+json application/merge-patch+json
Body Array of operations Any JSON value, usually an object
Addresses locations by JSON Pointer Mirroring the document’s shape
Set a value to null Yes No
Edit one array element Yes No, the array is replaced
Conditional check Yes, test No
Failure mode Whole patch rejected Nothing in the format can fail on its own

RFC 5789 suggests the status codes for a rejected patch: 400 for a malformed patch document, 415 for a patch format the server does not support (with an Accept-Patch header listing the ones it does), 422 when the patch is valid but cannot be applied, and 409 when it conflicts with the resource’s current state. A test failure is a reasonable fit for 409 or 422.

A third option you will meet in Kubernetes

kubectl patch takes --type json for RFC 6902, --type merge for JSON Merge Patch, and --type strategic, which is the default. Strategic merge patch is Kubernetes’ own format, not an IETF standard: it reads Merge Patch syntax but uses per-field metadata to merge certain lists instead of replacing them. The Kubernetes documentation notes that it “is not supported for custom resources”, so for a CRD you are back to choosing between the two standards above.

Which one to choose

  • Choose JSON Merge Patch for object-shaped resources edited a few fields at a time, where null is never a meaningful stored value and arrays are short enough to resend. It is easy to write by hand and easy to log.
  • Choose JSON Patch when clients edit inside large arrays, when null must be storable, when moves or copies are part of the domain, or when concurrent edits are possible and a test should guard the change.
  • Accept both if your clients vary. The media types are distinct, so one endpoint can serve both and advertise them in Accept-Patch.

Try both on your own documents

The fastest way to build intuition is to generate each kind of patch from the same pair of documents and compare them. Paste a before and after document into the JSON Patch tool to get an RFC 6902 patch, apply one, or reverse one to produce its undo. Paste the same pair into the JSON Merge tool and generate the merge patch: any key you set to null on the right side will come out looking exactly like a deletion, which is the limitation above made visible. To see which paths actually changed first, run the pair through JSON Diff, and use the JSON Pointer tool to check that a path resolves where you think it does. All of these run in your browser, with nothing uploaded, as described in how this site works.

JSON Patch vs Merge Patch FAQ

Is JSON Merge Patch the same as a deep merge? Close, but not identical. Many deep merge utilities store a null as an ordinary value, and some can concatenate arrays. Merge Patch deletes null keys and always replaces arrays. The JSON Merge tool keeps its configurable deep merge and its RFC merge patch generator as separate actions for that reason.

Can a JSON Patch path use wildcards or filters? No. Every path and from is a JSON Pointer, which names exactly one location. If you need “every item where status is draft”, find the indexes first with a query language; JSON Pointer vs JSONPath explains why patches deliberately use the stricter of the two.

Do I need a library for either format? Merge Patch is small enough to implement from the RFC’s pseudocode. JSON Patch has more edge cases (index shifting, -, move into a child, equality rules for test), so a maintained library is the safer choice.

Can I generate a patch by diffing two documents? Yes, for both, with one gap: a merge patch cannot express a value that changed to null, so a generator emits a deletion instead. JSON Patch generation is exact, though different tools may produce different but equivalent operation lists. To inspect the differences themselves, see how to compare two JSON documents.

Back to all posts