JSON Lark

Sheet 07 Patch

JSON Patch generator

Diff two JSON documents into an RFC 6902 patch, apply a patch to a document, or do the same with RFC 7396 merge patches.

Patch format
Action

Original

Modified

Patch

[
  { "op": "replace", "path": "/title", "value": "Field notebook, dotted" },
  { "op": "replace", "path": "/price", "value": 13 },
  { "op": "add", "path": "/tags/0", "value": "new" },
  { "op": "replace", "path": "/stock/eu", "value": 38 },
  { "op": "remove", "path": "/stock/us" },
  { "op": "replace", "path": "/dims~1cm/0", "value": 15 },
  { "op": "add", "path": "/discontinued", "value": false }
]
General notes

7 operations · applying them to the original gives the modified document

How to use it

Pick a format, then Generate to compare the original with the modified document, or Apply to run a patch against a document. Each generated patch is applied back to the original to check that it reproduces the modified document.

Array diffs
LCS leaves the common start and end of an array alone and finds the longest common subsequence of the rest, so inserting at the front is one add. Items changed in place are diffed recursively, so a new price inside an item is one replace. Index compares position by position: simpler, but larger patches.
JSON Pointer escaping
Paths are JSON Pointers: ~ is written ~0 and / is ~1, so the key dims/cm is /dims~1cm. In an add, - means the end of an array.
Test operations
Adds a test of the old value before each removal or replacement, so the patch fails cleanly if the document changed meanwhile.
Merge Patch
A merge patch is a plain object: members merge recursively, null removes a key and arrays are replaced whole. It cannot set a value to null; the status warns when that would be needed.

Questions

What happens when an operation fails?

Nothing is changed. Patches are applied to a copy, all or nothing, and the status names the failing operation and why: a failed test, a missing path, or an index past the end of an array.

Why does the generator never use move or copy?

Add, remove and replace always describe a change exactly, while guessing moves can produce surprising patches. Move and copy are fully supported when you apply a patch.

Are my documents uploaded?

No. Diffing and patching run in this tab and nothing you paste leaves it. Page views are counted with Cloudflare Web Analytics, which sets no cookies and never sees your data.