apply_patch
def apply_patch(document: Value, patch: Value) -> Value Apply a JSON Patch (RFC 6902) to a document.
The patch is a JSON array of operations. Each operation has:
- "op": The operation ("add", "remove", "replace", "move", "copy", "test")
- "path": JSON Pointer to the target location
- "value": The value (for add, replace, test)
- "from": Source path (for move, copy)
Operations are applied in order, and the whole patch either succeeds or changes nothing: the document is never edited in place, so an operation that raises leaves the caller's value as it was. The failure names the operation's index, because a patch that only says "key not found" is hard to place in a long list.
Example:
var doc = loads('{"name":"Alice"}')
var patch = loads('[{"op":"add","path":"/age","value":30}]')
var result = apply_patch(doc, patch)
Result is {"name":"Alice","age":30}.
- Args
document: The original JSON document.patch: Array of patch operations.