Apply JSON Patch operations
How JSON Patch operations change a JSON document, when a patch is the right tool, and how copying, formatting and tooling can affect the result.
JSON Patch is a standard format for describing changes to a JSON document. Instead of sending or storing a whole new version of the document, you write a short list of operations, such as add this field, remove that one, or replace this value, and a patch engine applies them in order. The format is defined in RFC 6902, and the paths it uses follow the JSON Pointer syntax from RFC 6901. This review explains what applying a patch actually changes, where the format works well, and where a plainer approach is easier to live with.
Is JSON Patch suitable for your use?
JSON Patch is a good fit when you need precise, machine-applied edits to structured data. Common examples are partial updates sent to an API, recorded change histories, and synchronizing a document between systems without resending all of it. Its main benefit is precision. Each operation names one exact location, and the optional test operation lets a patch refuse to run if the document is not in the expected state.
The main tradeoff is that a patch only makes sense against a specific starting document. Array positions are given by index, so a patch written for one version of a list can edit the wrong item, or fail, if the list has changed in the meantime. Patches are also harder for people to read than the finished document.
Plain alternatives are often the better choice in these situations:
- The document is small and replacing it wholesale is simpler and less error-prone.
- The audience is human reviewers who need to see what changed. A side-by-side text diff of the formatted document is usually easier to read.
- You only set or delete top-level fields and never need to change array items individually. JSON Merge Patch (RFC 7396) is simpler for that case, though it cannot set a value to null, because null means delete, and it replaces arrays as a whole.
- The change needs to be described in a ticket, changelog or message. A plain-language sentence is clearer than an operation list.
What applying a patch actually changes
A JSON Patch document is a JSON array of operation objects. Every operation has an "op" member and a "path" member. Depending on the operation, it also has a "value" member or a "from" member. The operations are applied one at a time, in order, and each one sees the result of the one before it. Under RFC 6902, if any operation fails, processing stops and the patch as a whole is not considered successful. Whether partial changes are rolled back depends on the implementation or the protocol around it. HTTP PATCH, for example, calls for the full set of changes to be applied atomically.
| Operation | Required members | Effect |
| add | path, value | Adds a member to an object, or replaces it if it already exists. Inserts into an array at an index, shifting later items. The index "-" appends to the end. |
| remove | path | Deletes the target. The target must exist. |
| replace | path, value | Replaces the value at the target. The target must exist. |
| move | from, path | Removes the value at "from" and adds it at "path". The "from" location cannot be a parent of "path". |
| copy | from, path | Adds a copy of the value at "from" to "path". |
| test | path, value | Changes nothing. Fails the patch if the value at the path is not equal to the given value. |
Paths are JSON Pointers. Each segment after a slash names an object member or an array index, and an empty path refers to the whole document. Two characters need escaping inside a segment: a tilde is written as ~0 and a slash as ~1. So the key "a/b" becomes the path /a~1b, and the key "m~n" becomes /m~0n. When decoding a path, ~1 is converted before ~0, so the sequence ~01 means a literal ~1 and not a slash.
The example below was put together for this article. The result was worked out by applying the RFC 6902 rules by hand, not by running a particular tool. Starting document:
{
"name": "Widget",
"tags": ["blue", "small"],
"price": 10,
"legacy_id": "w-01"
}
Patch:
[
{ "op": "test", "path": "/price", "value": 10 },
{ "op": "replace", "path": "/price", "value": 12 },
{ "op": "add", "path": "/tags/-", "value": "sale" },
{ "op": "remove", "path": "/legacy_id" },
{ "op": "copy", "from": "/name", "path": "/display_name" }
]
Result:
{
"name": "Widget",
"tags": ["blue", "small", "sale"],
"price": 12,
"display_name": "Widget"
}
Here is what each step did:
• The test confirmed the price was 10 before anything changed. If the price had been different, the whole patch would have failed.
• The add with "-" appended to the end of the array.
• If that path had been /tags/1 instead, "sale" would have been inserted between "blue" and "small".
It also helps to separate data changes from presentation. The patch changes stored values: members, array items and their contents. Indentation, line breaks and the order of object members are not part of that. JSON treats member order as insignificant, so a tool that applies a patch may print the result with different spacing or key order than the original. That changes how the document looks, not what it means. If you compare results with a plain text diff, format both sides the same way first, or the formatting noise will hide the real changes.
Will the result stay usable after copying or sharing?
A patch is ordinary JSON text, so it survives copy and paste as long as the characters arrive unchanged. The problems usually come from the editor or app in between, not from the format:
- Word processors and some chat or note apps turn straight quotes into curly quotes. That makes the JSON invalid.
- Invisible characters, such as non-breaking spaces copied from web pages, can break parsing or end up inside a key name, so the path no longer matches.
- Trailing commas and comments are not allowed in standard JSON. A patch that looks fine in a lenient editor may be rejected by a strict parser.
- RFC 8259 requires UTF-8 when JSON is exchanged between systems. Text saved in another encoding may not match keys that contain non-ASCII characters.
- Very large integers or high-precision decimals can lose precision in implementations that store all numbers as floating point. That can make a test operation fail unexpectedly, or change a replaced value.
Paths depend on the exact document they were written for. Sharing a patch without saying which document version it targets invites mistakes, especially when it uses array indices. Adding test operations for the values you expect makes a patch safer to pass around, because applying it to the wrong document will fail instead of quietly changing the wrong data.
For readability and accessibility, keep in mind that a patch describes changes in a compact, symbol-heavy form. Paths like /items/3/options/0 are exact but hard to scan, and they say nothing about why a change was made. No testing with screen readers or other assistive technology was done for this article, so no claim is made about how particular tools read patch documents aloud. When a change has to be understood by people, especially in documentation, reviews or support messages, add a plain-language description of the change or show the before and after documents.
Search and editing are affected in a similar way. Searching a patch for a field name only finds it if the name appears unescaped. A key containing a slash or tilde appears in escaped form inside the path, so a plain text search for the original key may miss it. Hand-editing a long patch is also risky, because later operations depend on the results of earlier ones. Inserting or deleting an array operation can shift the indices that the operations after it rely on.
Gaps and cautions
No specific JSON Patch tool, library or online service was evaluated for this article. The behavior described above is what the standards specify. Individual implementations may differ in how they report errors, whether they roll back partial changes, how they handle number precision, and how they format output, so check the documentation of the tool you plan to use.
Nothing here describes how any online patch tool handles your data. A tool's appearance or marketing does not tell you whether pasted documents are logged or kept. If your JSON contains credentials, personal data or internal configuration, use a library running locally, or confirm the service's documented data practices before pasting anything.