JSON Patch (RFC 6902) explained, with examples

short answer

A JSON Patch is a JSON array of operations that turns one JSON document into another. Each operation has an op (add, remove, replace, move, copy or test) and a path written as a JSON Pointer, such as /users/0/email.

Operations run in order, and the patch is all or nothing: if one operation fails, none of them apply. It is defined in RFC 6902 and sent with the media type application/json-patch+json.

A complete example

This patch changes an email address only if it still has the expected old value, grants a role, and swaps one preference for another.

Document
{
  "name": "Ada",
  "email": "ada@example.com",
  "roles": [
    "viewer"
  ],
  "prefs": {
    "theme": "light"
  }
}
Result
{
  "name": "Ada",
  "roles": [
    "viewer",
    "editor"
  ],
  "prefs": {
    "lang": "en-GB"
  },
  "email": "ada@lovelace.dev"
}
Patch
[
  {"op":"test","path":"/email","value":"ada@example.com"},
  {"op":"replace","path":"/email","value":"ada@lovelace.dev"},
  {"op":"add","path":"/roles/-","value":"editor"},
  {"op":"remove","path":"/prefs/theme"},
  {"op":"add","path":"/prefs/lang","value":"en-GB"}
]

The first operation is a test. If someone else changed the email in the meantime, the test fails and nothing is applied, which protects you from overwriting their change. Paste the document and the result into the compare tool to see the same changes as a diff.

The 6 operations

opWhat it doesExample and result
add Adds a member to an object, or inserts into an array. On an existing object key it overwrites the value. {"op":"add","path":"/tags/1","value":"b"} {"tags":["a","c"]} → {"tags":["a","b","c"]}
remove Removes the value. The path must exist. {"op":"remove","path":"/draft"} {"title":"Hi","draft":true} → {"title":"Hi"}
replace Replaces a value that must already exist. Same as remove then add at that path. {"op":"replace","path":"/title","value":"Hello"} {"title":"Hi"} → {"title":"Hello"}
move Removes the value at from and adds it at path. {"op":"move","from":"/old","path":"/new"} {"old":1} → {"new":1}
copy Copies the value at from to path. {"op":"copy","from":"/a","path":"/b"} {"a":{"x":1}} → {"a":{"x":1},"b":{"x":1}}
test Checks that the value at path equals value. If not, the whole patch fails. {"op":"test","path":"/version","value":3} {"version":3} → {"version":3}

add, replace and test need a value. move and copy need a from. Any other members of an operation object are ignored.

Paths are JSON Pointers

A path is a JSON Pointer (RFC 6901): each step starts with /, object keys are written as-is and array items by their index. /users/0/email is the email of the first user. The empty string "" means the whole document.

Two characters need escaping inside a key: ~ is written ~0 and / is written ~1. So the key "a/b" is /a~1b. Every difference listed by the compare tool shows its JSON Pointer when you hover over the path.

Array indexes and "-"

add at an index inserts and shifts later items right; it does not overwrite. To append, use - as the index: /roles/- adds after the last item. The index may equal the array length (that also appends) but not exceed it.

All or nothing

RFC 6902 says that if any operation fails, applying the patch as a whole fails. A server should leave the document unchanged and return an error. Combined with test, this gives you a simple form of optimistic locking without version headers.

Sending a patch over HTTP

Send it with the PATCH method and the JSON Patch media type, so the server knows it is a list of operations and not a partial document:

PATCH /api/users/42 HTTP/1.1
Content-Type: application/json-patch+json

[{"op": "replace", "path": "/email", "value": "ada@lovelace.dev"}]

If you only need to set and delete object fields, JSON Merge Patch is simpler and may be what the API expects. Check its documentation for which media type it accepts.

Mistakes that make a patch fail

  • Index past the end. Adding at /items/5 to a 1-item array fails ("Index 5 is out of bounds"). Use /items/- to append.
  • Missing parent. add creates the last step only. Adding /settings/theme to {} fails ("Path not found: /settings"); add /settings as an object first.
  • Forgetting that indexes shift. To delete "b" and "c" from ["a","b","c","d"], removing /list/1 then /list/2 gives ["a","c"]: the second operation sees the already shortened array and deletes "d". Remove from the highest index down.
  • replace on a key that does not exist. It fails; use add, which creates or overwrites.
  • Sending a bare object. A JSON Patch is always an array, even with a single operation.

Every result shown in this guide was computed when the page was built, by the same code that runs the JSON compare tool, and is checked by automated tests.