JSON Patch: Apply and Generate RFC 6902 Patches

Apply a JSON Patch to a document and see the result, or paste two versions of a document and get the patch that turns one into the other. Merge Patch (RFC 7396) is supported too. Each operation is checked and failures name the exact operation and JSON Pointer. Nothing leaves your browser.

Result

        

What is JSON Patch?

JSON Patch, defined in RFC 6902, is a format for describing changes to a JSON document. A patch is an array of operations, each with an op, a path written as a JSON Pointer (RFC 6901), and usually a value. Instead of sending a whole updated document, a client sends only the changes. That is why JSON Patch is the body format for HTTP PATCH requests with the media type application/json-patch+json, and why Kubernetes (kubectl patch --type json), ASP.NET Core and many REST APIs accept it.

How to use this JSON Patch tool

  1. Apply: paste the original document and a patch array. The result appears on the right, and the log under the editors shows each operation as it is applied.
  2. Generate: switch to Generate patch, paste the old document on the left and the new one in the middle. The tool writes the shortest list of add, remove and replace operations that turns one into the other.
  3. Merge Patch: switch to Apply Merge Patch to use the simpler RFC 7396 format, where you send a partial object and null deletes a key.

The six JSON Patch operations

OperationExampleEffect
add{"op":"add","path":"/tags/-","value":"new"}Adds a member, inserts into an array at an index, or appends with -. Replaces an existing object member.
remove{"op":"remove","path":"/draft"}Removes the value. The path must exist.
replace{"op":"replace","path":"/price","value":12}Replaces an existing value. Fails if the path does not exist.
move{"op":"move","from":"/a","path":"/b"}Removes the value at from and adds it at path.
copy{"op":"copy","from":"/a","path":"/b"}Copies the value at from to path.
test{"op":"test","path":"/version","value":3}Fails the whole patch unless the value equals the given value. Used for optimistic concurrency.

Patches are atomic: if any operation fails, the document is left unchanged. This tool follows the same rule. It applies operations to a copy and only shows a result when every operation succeeds.

JSON Pointer paths in a patch

Paths use JSON Pointer syntax: / separates keys, array items are addressed by index (/items/0), and - means "after the last item" for add. Two characters are escaped: ~ is written ~0 and / is written ~1. So the key a/b is the path /a~1b. The empty path "" refers to the whole document.

Document:  {"users": [{"name": "Ada"}], "a/b": 1}
/users/0/name   → "Ada"
/users/-        → append position for add
/a~1b           → 1

Example: apply a patch

Original: {"name": "Widget", "price": 10, "tags": ["tools"]}
Patch:
[
  {"op": "test",    "path": "/price", "value": 10},
  {"op": "replace", "path": "/price", "value": 12},
  {"op": "add",     "path": "/tags/-", "value": "sale"},
  {"op": "add",     "path": "/stock", "value": 40}
]
Result: {"name": "Widget", "price": 12, "tags": ["tools", "sale"], "stock": 40}

JSON Patch vs JSON Merge Patch

JSON Merge Patch (RFC 7396) is simpler: you send a partial document, and its keys are merged into the target recursively. A key set to null is deleted. The trade-off is that merge patch cannot set a value to null, cannot change one item inside an array (arrays are replaced whole), and has no test operation. Use JSON Patch when you need precise array edits, moves or concurrency checks; use Merge Patch for simple partial updates of objects. Its media type is application/merge-patch+json.

Target: {"title": "Hello", "author": {"name": "Ada", "email": "x"}, "tags": ["a", "b"]}
Merge patch: {"author": {"email": null}, "tags": ["c"]}
Result: {"title": "Hello", "author": {"name": "Ada"}, "tags": ["c"]}

Common JSON Patch errors

JSON Patch in code

// JavaScript: fast-json-patch
import { applyPatch, compare } from 'fast-json-patch';
const patch = compare(oldDoc, newDoc);
const next = applyPatch(doc, patch).newDocument;

# Python: jsonpatch
import jsonpatch
patch = jsonpatch.make_patch(old, new)
result = patch.apply(doc)

# Kubernetes
kubectl patch deployment web --type json \
  -p '[{"op":"replace","path":"/spec/replicas","value":3}]'

To see differences side by side rather than as operations, use the JSON diff checker. To merge several files without writing a patch, use JSON merge.

Frequently asked questions

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

JSON Patch (RFC 6902) is a list of operations such as add, remove, replace, move, copy and test, addressed with JSON Pointer paths. JSON Merge Patch (RFC 7396) is a partial document merged into the target, where null deletes a key. Merge Patch is simpler but cannot edit single array items or set a value to null.

How do I append to an array with JSON Patch?

Use an add operation with - as the last path segment, for example {"op":"add","path":"/tags/-","value":"new"}.

How do I escape a slash or tilde in a JSON Patch path?

Write ~1 for / and ~0 for ~. The key a/b becomes /a~1b.

What happens if one operation in a patch fails?

The whole patch fails and the document is not changed. RFC 6902 requires patches to be applied atomically, and this tool behaves the same way, naming the operation that failed.

How do I generate a JSON Patch from two JSON documents?

Switch to Generate patch, paste the original document and the changed document, and the tool produces the add, remove and replace operations that turn the first into the second. Tick the option to include test operations for safer updates.

Is my data uploaded?

No. Applying and generating patches runs in JavaScript in your browser, so your documents stay on your device.