JSON Patch (RFC 6902) Generator
Diff two JSON documents into a machine-applicable patch, then prove it by replaying every operation locally.
A · Before
5 lines · 81 charsB · After
5 lines · 84 charsRFC 6902 patch
4 operationsPatch document
[
{
"op": "replace",
"path": "/meta/a~1b",
"value": 3
},
{
"op": "replace",
"path": "/name",
"value": "Grace"
},
{
"op": "remove",
"path": "/tags/1"
},
{
"op": "add",
"path": "/tags/1",
"value": "public"
}
]import { applyPatch } from "fast-json-patch";
const document = /* document A */;
const patch = "[\n {\n \"op\": \"replace\",\n \"path\": \"/meta/a~1b\",\n \"value\": 3\n },\n {\n \"op\": \"replace\",\n \"path\": \"/name\",\n \"value\": \"Grace\"\n },\n {\n \"op\": \"remove\",\n \"path\": \"/tags/1\"\n },\n {\n \"op\": \"add\",\n \"path\": \"/tags/1\",\n \"value\": \"public\"\n }\n]";
const { newDocument } = applyPatch(document, JSON.parse(patch));fast-json-patch applies all six RFC 6902 operations, including move and test.
Examples
Replace a nested field
A: {"name":"Ada"} B: {"name":"Grace"}[{"op":"replace","path":"/name","value":"Grace"}]A single changed member becomes one replace operation.
Insert into an array
A: ["a","b","d"] B: ["a","b","c","d"][{"op":"add","path":"/2","value":"c"}]The insertion index is computed against the state at the moment the operation is applied.
Escape a slash in a key
A: {"a/b":1} B: {"a/b":2}[{"op":"replace","path":"/a~1b","value":2}]RFC 6901 escapes the slash inside the token while keeping the pointer separator intact.
Reorder with a move
A: [{"id":1},{"id":2},{"id":3}] B: [{"id":3},{"id":1},{"id":2}][{"op":"move","from":"/2","path":"/0"}]Minimal mode recognises the equal object that changed position; compatibility mode emits remove + add instead.
About this tool
RFC 6902 defines a small document format for describing changes to JSON: an ordered array of operations such as replace, add, remove and move. This tool diffs a before document (A) against an after document (B) and emits that patch document, ready to paste into a PATCH request, a test fixture or a library call.
Objects are compared key by key with sorted, deterministic ordering, so the same input pair always produces the same patch. Arrays are aligned with a longest-common-subsequence pass; operations are emitted in the order a conforming applier executes them, which keeps indices valid as earlier edits shift the array. Optional move detection recognises values that changed position instead of treating them as a deletion plus an insertion.
Correctness is proven rather than assumed: the patch is replayed against document A with a built-in RFC 6902 applier, and the result must deep-equal document B before the patch is shown. If an optimisation cannot be verified, the tool falls back to a verified compatibility patch or reports the failure explicitly. Safety mode adds test operations that guard the values a mutation touches, useful with optimistic concurrency and If-Match headers.
RFC 6901 JSON Pointer escaping is applied to every token: a slash in a key becomes ~1 and a tilde becomes ~0. Integer tokens beyond double precision are preserved verbatim, so large IDs and nanosecond timestamps are not silently rounded. The documents never leave the browser tab; there is no upload, account or network request.
What this tool does not do
- This is RFC 6902 JSON Patch only; RFC 7386 JSON Merge Patch is not generated.
- Arrays are matched by deep equality, not by a configurable identity field such as id; an item that is both edited and moved may appear as remove + add.
- Arrays longer than 2000 items use index-wise comparison instead of LCS; a warning is shown and the patch is still verified.
- Paste-based input only: there is no file picker or drag-and-drop in this version.
- Patch replay is synchronous on the main thread; very large documents can briefly delay the UI.
- Duplicate object keys are resolved with JSON.parse's last-wins rule and reported as a warning; the original duplicate text cannot be preserved in the patch.
How to use this tool
Paste the original
Put the starting JSON in the left pane (A). Use Format to pretty-print it, or load one of the examples.
Paste the desired document
Put the target JSON in the right pane (B). Both panes are labelled and validated separately.
Choose an emission mode
Compatibility emits only add/remove/replace for naive appliers; Minimal + moves shortens arrays and object reorders with verified move operations.
Enable safety tests if needed
Safety mode inserts test operations before mutations that have a meaningful value to assert. Pair the patch with If-Match when sending it over HTTP.
Copy or download the patch
Check the VERIFIED badge and operation counts, then copy the patch or a library snippet. Mod+Shift+Enter copies the patch from anywhere in the tool.
Use cases
Implement a PATCH endpoint
Turn before/after payloads into a concrete RFC 6902 body and test your server's applier against it.
Generate test fixtures
Export a verified patch document for regression tests of state transitions in JSON:API clients.
Prepare Kubernetes updates
Use the kubectl --type=json form of a patch and see how it differs from strategic merge patch.
Debug index-shift bugs
Compare your hand-written patch with one generated in application order and verified by replay.
RFC 6902 operations emitted by this tool
| Operation | Meaning in the generated patch |
|---|---|
| add | Insert an object member or a single array element at a JSON Pointer; /- appends when Prefer append is on |
| remove | Delete an existing member or array element; array removals are emitted bottom-up so earlier indices stay valid |
| replace | Set an existing member, element or the whole document when the pointer is empty |
| move | Relocate a value from a from pointer to a path pointer, used only in minimal mode and only when replay verifies |
| test | Safety-mode guard asserting the current value at a path before a mutation; a failure means the document changed underneath the client |
| copy | Not emitted by the generator, but fully supported by the built-in applier for verifying external patches |
RFC 6901 pointer tokens escape ~ as ~0 and / as ~1; unescaping happens in that order. The six operations above are the complete RFC 6902 set.
Common mistakes
Mistake:Adding an array with one add operation whose value is the whole array.
Fix:RFC 6902 add inserts a single element. Populate an empty array with one add per element, or use /- to append.
Mistake:Assuming the default patch uses move operations because they are shorter.
Fix:Compatibility mode is the default and emits only add/remove/replace. Enable Minimal + moves only when your applier supports move.
Mistake:Placing a test operation at the path an add will create.
Fix:A test at a nonexistent path fails. Safety mode tests the existing target for replace/remove, the source for move, and the parent for add.
Mistake:Treating JSON Patch and JSON Merge Patch as interchangeable.
Fix:RFC 6902 is an ordered operation list; RFC 7386 is a whole-document object. This tool emits only RFC 6902.
Frequently asked questions
What is the difference between RFC 6902 and RFC 7386?
RFC 6902 JSON Patch is an ordered array of operations (add, remove, replace, move, copy, test) applied to a JSON document with JSON Pointer paths. RFC 7386 JSON Merge Patch is a single object whose keys are merged into the target, where null means delete. This tool generates RFC 6902 patches only, because operation order and array indices matter for PATCH endpoints and test fixtures.
How are array indices handled?
Operations are emitted in application order. Plain removals go from the highest index down, then remaining items are placed from left to right, so an earlier insertion never invalidates a later index. When Prefer append is enabled, insertions at the end of an array use the - token; populating an empty array emits one add per element, never one add containing the whole array.
What does the test operation do?
A test operation asserts that the value at a JSON Pointer equals the supplied value; if it does not, the applier must reject the patch. In safety mode the tool inserts test operations before mutations: replace and remove test the target, move tests its source, and add tests its parent container because the new target does not exist yet. This is the standard guard against concurrent edits, usually paired with an If-Match header over HTTP.
How do I escape a key containing / or ~?
RFC 6901 replaces ~ with ~0 and then / with ~1 inside each pointer token. A key like a/b becomes the path /a~1b. The generator escapes keys automatically and the built-in applier unescapes in the reverse order, so round-trips are verified before the patch is shown.
Is the generated patch verified?
Yes. The tool replays the patch against document A with its own RFC 6902 applier and compares the result with document B. Only a patch that passes this check shows the VERIFIED badge. If move detection or safety guards cannot be verified, the tool keeps the verified compatibility patch and tells you why.
Why does compatibility mode avoid move operations?
Some appliers implement add, remove and replace but not move. Compatibility mode is the default for that reason. Switch to Minimal + moves when your consumer supports RFC 6902 fully and you want shorter patches for reordered arrays or renamed keys.
Are large IDs and timestamps preserved exactly?
Integer tokens beyond JavaScript's safe integer range are kept as their original JSON text and emitted unquoted in the patch. Without this, two different snowflake IDs could look equal after parsing and produce a misleading empty patch. Strings are never coerced to numbers.
Does my JSON leave the browser?
No. Parsing, diffing, replay verification and exports all happen locally in this tab. Only your option choices are stored in localStorage; documents are not persisted or uploaded.
References & standards
Related tools
API Mock Server
Define mock REST routes and intercept fetch calls client-side
AsyncAPI Validator
Validate AsyncAPI 2.x and 3.x documents offline, browse channels and messages, and check protocol binding coverage.
Bandwidth Calculator
Estimate how long a file takes to transfer at a given connection speed — correctly handling bits vs bytes (Mbps vs MB/s) and SI vs binary size units
Bandwidth-Delay Product Calculator
Compute BDP from bandwidth and RTT: required TCP window, scaling factor, in-flight MSS and buffer notes. Auditable math, entirely in your browser.
Broken Link Checker
Extract http(s) links from pasted HTML and check them via proxy
Bulk Redirect Map Generator
Generate nginx map blocks, Apache RedirectMatch rules, Netlify _redirects and vercel.json from one old-to-new URL list, with status codes, wildcards and conflict detection.