json_mlake ยท API

json.patch

Functions

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.

merge_patch

def merge_patch(target: Value, patch: Value) -> Value

Apply a JSON Merge Patch (RFC 7396) to a document.

Merge patch rules:

  • If patch is not an object, it replaces target entirely
  • null values in patch remove keys from target
  • Other values are recursively merged

Example: var target = loads('{"a":1,"b":2}') var patch = loads('{"b":null,"c":3}') var result = merge_patch(target, patch) Result is {"a":1,"c":3}.

Args
target: The original document.
patch: The merge patch to apply.

create_merge_patch

def create_merge_patch(source: Value, target: Value) -> Value

Create a merge patch that transforms source into target.

Example: var source = loads('{"a":1,"b":2}') var target = loads('{"a":1,"c":3}') var patch = create_merge_patch(source, target) Result is {"b":null,"c":3}.

Args
source: The original document.
target: The desired result.