JSON Patch (RFC 6902)

Build the JSON Patch that turns one JSON document into another, or apply a patch to a document.

loading JSON Patch (RFC 6902)
About this tool

What is JSON Patch?

JSON Patch, defined in RFC 6902, is a way to describe a change to a JSON document as a list of small operations. Instead of sending the whole new document, you send something like "replace the value at /age with 31" and "add /vip". It is used in HTTP PATCH requests, in Kubernetes, in sync protocols and in audit logs, where you want to know exactly what changed.

A patch is an array of objects. Each object has an op, which is one of add, remove, replace, move, copy or test, a path that points to a place in the document, and, depending on the operation, a value or a from. The paths are JSON Pointers, defined in RFC 6901: they start with /, each step is a key or an array index, and the characters ~ and / inside a key are written ~0 and ~1.

This tool does two jobs. It compares two documents and builds the patch that turns the first into the second. And it applies a patch to a document and shows the result. When you build a patch you can turn on "test old values first": for each change, a test operation is added before it, so that the patch fails if the document is no longer what you expected. That is the cheap way to avoid overwriting somebody else's change. Everything runs in your browser.

How to make or apply a patch

  1. Choose the mode: "make a patch" or "apply a patch".
  2. To make a patch, paste the original JSON in "before" and the changed JSON in "after". The patch appears at once, with the number of operations.
  3. Turn on "test old values first" if the patch will be applied to a document that may have changed in the meantime.
  4. To apply a patch, paste the document and the patch, which must be an array of operations. The patched document appears on the right.
  5. Choose the indent, or minified output.
  6. If an operation fails, the message names the operation by number and the path. The document is never half-changed: either all the operations apply or none.

Examples

Making a patch

Before:

{"name":"Ana","age":30,"tags":["a","b"],"address":{"city":"Lisboa"}}

After:

{"name":"Ana","age":31,"tags":["a","c","d"],"address":{"city":"Porto"},"vip":true}

Patch:

[
  { "op": "replace", "path": "/age", "value": 31 },
  { "op": "replace", "path": "/tags/1", "value": "c" },
  { "op": "add", "path": "/tags/2", "value": "d" },
  { "op": "replace", "path": "/address/city", "value": "Porto" },
  { "op": "add", "path": "/vip", "value": true }
]

The tool shows the same operations with one field per line. Unchanged keys such as name do not appear. Array positions are compared one by one.

The same patch with the old values tested

With "test old values first", every replace is preceded by a test of the value that is being replaced:

[
  { "op": "test", "path": "/age", "value": 30 },
  { "op": "replace", "path": "/age", "value": 31 },
  { "op": "test", "path": "/tags/1", "value": "b" },
  { "op": "replace", "path": "/tags/1", "value": "c" },
  { "op": "add", "path": "/tags/2", "value": "d" },
  { "op": "test", "path": "/address/city", "value": "Lisboa" },
  { "op": "replace", "path": "/address/city", "value": "Porto" },
  { "op": "add", "path": "/vip", "value": true }
]

If someone changed /age to 40 in the meantime, the first test fails, the patch stops and nothing is applied. Adding a new key needs no guard, because nothing old is being overwritten.

Applying a patch and what can go wrong

Applying the first patch to the "before" document gives the "after" document:

{
  "name": "Ana",
  "age": 31,
  "tags": ["a", "c", "d"],
  "address": { "city": "Porto" },
  "vip": true
}

If the patch is [{"op":"test","path":"/age","value":99},{"op":"replace","path":"/age","value":1}], the tool stops at operation 1: the value at /age is not the expected one. If it is [{"op":"remove","path":"/zzz"}], it says that nothing exists at /zzz. And when you compare {"a/b":1} with {"a/b":5}, the key with a slash is written with the escape:

[{ "op": "replace", "path": "/a~1b", "value": 5 }]

Common mistakes

  • Confusing a patch with a diff for people. A JSON Patch is meant for programs. To read the differences, use JSON diff, which shows them side by side.
  • Forgetting that add on an array inserts. add at /tags/1 pushes the existing items to the right. It does not replace. Use /tags/- to append at the end.
  • Writing the path without the leading slash. name is not a pointer. The path is /name, and the whole document is the empty path.
  • Not escaping / and ~ in keys. A key a/b is /a~1b, and a key m~n is /m~0n. The order matters: ~ first, then /.
  • Expecting the patch to be minimal for arrays. Items are compared by position, so removing the first element of a long list can produce many replace operations instead of one remove.
  • Applying a patch to the wrong version. Use the "test old values first" option when a document can change between the moment you build the patch and the moment you apply it.

Frequently asked questions

What is the difference between JSON Patch and JSON Merge Patch?

JSON Patch is a list of explicit operations (RFC 6902). JSON Merge Patch (RFC 7386) is a partial document that is merged into the target. This tool is only about JSON Patch.

Does applying a patch change my original document?

No. The result is a new document shown on the right. If any operation fails, you get an error and no result, never a half-applied one.

Can I use JSON Patch with a JSONPath expression?

No. A patch uses JSON Pointer paths, which point to one exact place. To search for values with filters first, try the JSONPath tester, then write the pointer by hand.

How do I check the document after patching?

Use the JSON Schema validator with your schema to confirm that the result is still valid.

Report a problem