The Problem JSON Patch Solves
Updating part of a resource over HTTP has three common approaches, and two of them have real problems. Resending the entire resource (PUT) works but wastes bandwidth and risks clobbering a field someone else updated concurrently. A bespoke "partial update" JSON body works until every client and server has to agree on an undocumented convention for what a missing field means versus an explicit null. JSON Patch (RFC 6902) is the standardized middle ground: an explicit, ordered list of operations describing exactly what changed.
The Six Operations
[
{ "op": "add", "path": "/active", "value": true },
{ "op": "remove", "path": "/legacyField" },
{ "op": "replace", "path": "/role", "value": "senior engineer" },
{ "op": "move", "from": "/tempName", "path": "/name" },
{ "op": "copy", "from": "/address", "path": "/billingAddress" },
{ "op": "test", "path": "/version", "value": 3 }
]
| Operation | What it does |
|---|---|
add | Inserts a value at path. On an object, creates or overwrites a key. On an array, inserts at that index (shifting later elements right). |
remove | Deletes the value at path. |
replace | Equivalent to a remove followed by an add at the same path — the target must already exist. |
move | Removes the value at from and adds it at path — a single atomic operation, not two separate ones you write yourself. |
copy | Like move, but leaves the original at from untouched. |
test | Asserts the value at path equals the given value — if it doesn't match, the entire patch application fails before any further operations run. |
Paths Are JSON Pointers (RFC 6901), Not JSONPath
This trips people up specifically because both look similar and both show up in JSON tooling. A JSON Patch path is a JSON Pointer:
/user/address/city → object property access, slash-separated
/items/2 → array index (zero-based)
/items/- → "append" — only valid with add
/a~1b → the literal key "a/b" — ~1 escapes a literal slash
/m~0n → the literal key "m~n" — ~0 escapes a literal tilde
JSONPath ($.user.address.city, used by tools like the JSONPath Tester) is a separate, more expressive query language for finding values. JSON Pointer is deliberately simpler — it addresses exactly one location, with no wildcards or filtering — which is exactly the precision a patch operation needs.
Writing the Actual HTTP Request
PATCH /api/users/42 HTTP/1.1
Content-Type: application/json-patch+json
[
{ "op": "replace", "path": "/role", "value": "senior engineer" },
{ "op": "add", "path": "/tags/-", "value": "rust" }
]
The Content-Type: application/json-patch+json header matters — it's a distinct media type from application/json, and a server implementing RFC 6902 correctly may reject a patch body sent with the generic JSON content type, since that's conventionally reserved for a full-resource representation (what a PUT would send) or a JSON Merge Patch.