JSON Merge Patch reads like the document but cannot set a null or edit one array element; JSON Patch can, at the cost of atomicity rules and index gotchas. Here is how each one breaks and when to use which.
Most PATCH endpoints accept "a JSON object with the fields you want to change" and never say which spec that is, which is how teams end up with an API that cannot clear a field, silently drops array items under concurrent edits, or treats null three different ways in three handlers. There are two standardized answers, JSON Merge Patch (RFC 7396) and JSON Patch (RFC 6902), and they make opposite tradeoffs: one reads like the document, the other reads like a list of instructions.
This post walks through both on the same document, shows the exact cases where each one breaks, and ends with a short rule for choosing.
Here is the resource we will patch, a user profile:
{
"name": "Ada",
"nickname": "countess",
"address": { "city": "London", "zip": "W1" },
"tags": ["admin", "beta", "staff"],
"version": 7
}
The edit we want: change the city to Cambridge, remove the zip, set nickname to an explicit null (the UI shows "no nickname" differently from "never set"), and remove the beta tag. Watch how each format handles each of those four changes.
RFC 7396 defines a patch as a JSON document shaped like the target. The rules fit in one paragraph: for each key in the patch, if the value is null, delete that key from the target; if the value is an object, recurse into it; otherwise, replace the target's value with the patch's value. If the patch itself is not an object (an array, a string, a number), it replaces the whole target. The media type is application/merge-patch+json.
So the city and zip changes are easy:
{
"address": { "city": "Cambridge", "zip": null }
}
That is the whole appeal. A frontend can build this by serializing the fields of a form that changed, a human can read it in a log line, and applying it twice gives the same result as applying it once, so merge patch requests are naturally idempotent.
You cannot set a value to null. null in a merge patch means "delete". There is no way to say "set nickname to the JSON value null". If your schema distinguishes "absent" from "explicitly null" (and many do, for example a deleted_at timestamp or a nullable foreign key), merge patch can only express one of them. The usual workaround is a sentinel like an empty string, which then leaks into every client.
Arrays are replaced wholesale. Merge patch has no syntax for "remove one element". To drop beta, the client sends the entire new array:
{ "tags": ["admin", "staff"] }
That is fine for a single editor. It is a lost update the moment two clients edit the list concurrently: client A removes beta, client B adds ops, both read version 7, and whichever request lands second overwrites the other's change. Nothing in the format tells the server that the client's view was stale.
A non-object patch replaces everything. Sending [] or "x" as a merge patch is legal and replaces the whole resource. A buggy client that sends the wrong body shape does not get a 400 by default; it gets data loss, unless the server validates the patch shape before applying it.
RFC 6902 takes the other approach. A patch is an ordered array of operations, each with an op, a path written as a JSON Pointer (RFC 6901), and sometimes a value or from. There are six operations: add, remove, replace, move, copy and test. The media type is application/json-patch+json.
The same four edits, all expressible:
[
{ "op": "test", "path": "/version", "value": 7 },
{ "op": "replace", "path": "/address/city", "value": "Cambridge" },
{ "op": "remove", "path": "/address/zip" },
{ "op": "replace", "path": "/nickname", "value": null },
{ "op": "test", "path": "/tags/1", "value": "beta" },
{ "op": "remove", "path": "/tags/1" }
]
Three things merge patch could not do are now explicit. nickname is set to a real null with replace. One array element is removed by index. And the test operations turn the patch into a conditional write: if version is no longer 7, or index 1 is no longer beta because someone else edited the list, the whole patch fails instead of deleting the wrong tag.
Patches are atomic. RFC 6902 says operations apply in order and that if any operation fails, the patch as a whole is not successful, and RFC 5789, which defines the HTTP PATCH method, requires the server to apply the changes atomically. A server that applies operations one by one against the live record and stops at the first error, leaving the first three applied, is not compliant. Apply to a copy, then commit.
add is overloaded. On an object member, add creates the key or replaces it if it already exists. On an array, add at index i inserts and shifts later elements right; it does not overwrite. Appending uses the special index -, as in "/tags/-". replace, by contrast, requires the target to exist and fails otherwise.
Indexes move as you go. Each operation sees the document left by the previous one. Two remove operations at /tags/1 remove two different elements. Generators usually emit array removals from the highest index down to avoid surprises.
Pointer escaping. A key containing / or ~ must be escaped: ~1 for / and ~0 for ~. A map keyed by URL path, such as {"/api/users": ...}, is addressed as /~1api~1users. Forgetting this produces patches that point at the wrong place without any parse error.
Not idempotent by default. add at /tags/- applied twice appends twice. Combine JSON Patch with a test on a version field, an If-Match ETag, or an idempotency key if clients retry.
| JSON Merge Patch (RFC 7396) | JSON Patch (RFC 6902) | |
|---|---|---|
| Media type | application/merge-patch+json | application/json-patch+json |
| Shape | Partial document | Array of operations |
| Delete a key | Set it to null | remove op |
Set a key to null | Not possible | replace with "value": null |
| Edit one array element | Not possible, resend the array | add, remove, replace by index |
| Move or copy values | No | move, copy |
| Built-in precondition | No | test op |
| Idempotent | Yes | Not in general |
| Atomicity | Trivially, it is one merge | Required, all operations or none |
| Easy to hand-write | Very | Moderately |
| Easy to generate from a diff | Yes | Yes, but generated patches are rarely minimal |
The row that decides most designs is "edit one array element". If your resources contain arrays that more than one client edits, merge patch forces whole-array replacement and you need a concurrency control layer on top. If they do not, merge patch's simplicity usually wins.
Neither format solves lost updates on its own. The standard HTTP mechanism is a conditional request from RFC 9110: the server returns an ETag on GET, the client sends it back in If-Match on PATCH, and the server responds 412 Precondition Failed if the resource changed in between. The same ETag also powers read-side revalidation, as covered in HTTP caching with ETags and 304 Not Modified.
With If-Match, merge patch becomes safe even for arrays: the second writer is rejected rather than silently overwriting, and the client re-reads and rebuilds its edit. JSON Patch's test operation is a finer-grained version of the same idea. It can guard only the field being changed, so two clients editing unrelated parts of a large document do not conflict, at the cost of a longer patch.
A practical split: use ETags for whole-resource concurrency on every PATCH endpoint, and reach for test when documents are large, hot and edited field by field.
Kubernetes is the most visible API that supports both standard formats, plus two of its own. kubectl patch --type=json sends RFC 6902, --type=merge sends RFC 7396, and the default, --type=strategic, sends a strategic merge patch. The Kubernetes patch documentation walks through all three.
Strategic merge patch exists precisely because of the array problem. A Pod's containers list is an array, and replacing it wholesale every time you change one image would be absurd. So Kubernetes annotates its Go types with a merge key (for containers, name), and a strategic patch that mentions a container named web merges into that element instead of replacing the list. It works well, but it only works because the server knows the schema; it is not something you can apply generically to arbitrary JSON. Server-side apply, the newer mechanism, goes further and tracks which manager owns which field. If you have been bitten by YAML in these manifests, the YAML whitespace footguns are a separate and equally fun problem.
The lesson for your own API is the general one: if your arrays are really keyed collections (line items by sku, members by user_id), consider modeling them as objects keyed by ID, or as sub-resources with their own endpoints. Then merge patch handles them cleanly and you never need a strategic merge format.
Whichever format you pick, a few server-side habits prevent most incidents:
Content-Type. Return 415 Unsupported Media Type for anything else, and advertise what you accept with the Accept-Patch response header that RFC 5789 defines. Do not guess the format from the body shape./id, /created_at or /version before applying, rather than letting them through and overwriting server-owned fields.test). 409 Conflict or 422 Unprocessable Content are both common choices for a patch that is well formed but cannot be applied; pick one and document it.application/json. Spec-driven teams get this for free if they treat the OpenAPI document as the contract.Mature libraries exist for both formats in every mainstream language, so there is no reason to hand-roll the apply logic. When debugging, it helps to see the operations a diff would produce; a JSON Patch diff and apply tool is handy for checking what a client is actually sending against a real document.
Default to JSON Merge Patch with If-Match for ordinary CRUD resources: it is readable, idempotent and trivially generated from a form. Switch to JSON Patch when you need any of the three things merge patch cannot express (an explicit null, a single array element edit, or a field-level precondition), and remember that it must be applied atomically. Whatever you pick, put the media type in the contract, because "a JSON object with the changed fields" is not a spec, it is a bug report waiting to be filed.
Free, in your browser, no sign-up.
One email when a new post goes up. No other mail; unsubscribe in one click.
We store your email only to send new posts. See the privacy policy.