dictutils.ops.patch

dictutils.ops.patch(d, ops)[source]

Apply JSON-Patch-like operations to an object.

Parameters:
  • d (Any) – Object to patch

  • ops (Sequence[Mapping[str, Any]]) – List of operation dictionaries with “op”, “path”, and optionally “value”

Return type:

Any

Returns:

The patched object

Example

>>> import json
>>> data = {"user": {"name": "Alice", "age": 30}}
>>> operations = [
...     {"op": "replace", "path": "user.age", "value": 31},
...     {"op": "add", "path": "user.email", "value": "alice@example.com"},
...     {"op": "remove", "path": "user.age"}
... ]
>>> result = patch(data, operations)
>>> print(json.dumps(result, indent=4))
{
    "user": {
        "name": "Alice",
        "email": "alice@example.com"
    }
}