The jsonpatch module¶
Apply JSON-Patches (RFC 6902)
-
class
jsonpatch.AddOperation(operation, pointer_cls=<class 'jsonpointer.JsonPointer'>)¶ Adds an object property or an array element.
-
class
jsonpatch.CopyOperation(operation, pointer_cls=<class 'jsonpointer.JsonPointer'>)¶ Copies an object property or an array element to a new location
-
exception
jsonpatch.InvalidJsonPatch¶ Raised if an invalid JSON Patch is created
-
exception
jsonpatch.JsonPatchConflict¶ Raised if patch could not be applied due to conflict situation such as: - attempt to add object key when it already exists; - attempt to operate with nonexistence object key; - attempt to insert value to array at position beyond its size; - etc.
-
exception
jsonpatch.JsonPatchException¶ Base Json Patch exception
-
exception
jsonpatch.JsonPatchTestFailed¶ A Test operation failed
-
class
jsonpatch.MoveOperation(operation, pointer_cls=<class 'jsonpointer.JsonPointer'>)¶ Moves an object property or an array element to a new location.
-
class
jsonpatch.PatchOperation(operation, pointer_cls=<class 'jsonpointer.JsonPointer'>)¶ A single operation inside a JSON Patch.
-
apply(obj)¶ Abstract method that applies a patch operation to the specified object.
-
-
class
jsonpatch.RemoveOperation(operation, pointer_cls=<class 'jsonpointer.JsonPointer'>)¶ Removes an object property or an array element.
-
class
jsonpatch.ReplaceOperation(operation, pointer_cls=<class 'jsonpointer.JsonPointer'>)¶ Replaces an object property or an array element by a new value.
-
class
jsonpatch.TestOperation(operation, pointer_cls=<class 'jsonpointer.JsonPointer'>)¶ Test value by specified location.
-
jsonpatch.apply_patch(doc, patch, in_place=False, pointer_cls=<class 'jsonpointer.JsonPointer'>)¶ Apply list of patches to specified json document.
Parameters: - doc (dict) – Document object.
- patch (list or str) – JSON patch as list of dicts or raw JSON-encoded string.
- in_place (bool) – While
Truepatch will modify target document. By default patch will be applied to document copy. - pointer_cls (Type[JsonPointer]) – JSON pointer class to use.
Returns: Patched document object.
Return type: dict
>>> doc = {'foo': 'bar'} >>> patch = [{'op': 'add', 'path': '/baz', 'value': 'qux'}] >>> other = apply_patch(doc, patch) >>> doc is not other True >>> other == {'foo': 'bar', 'baz': 'qux'} True >>> patch = [{'op': 'add', 'path': '/baz', 'value': 'qux'}] >>> apply_patch(doc, patch, in_place=True) == {'foo': 'bar', 'baz': 'qux'} True >>> doc == other True
-
jsonpatch.make_patch(src, dst, pointer_cls=<class 'jsonpointer.JsonPointer'>)¶ Generates patch by comparing two document objects. Actually is a proxy to
JsonPatch.from_diff()method.Parameters: - src (dict) – Data source document object.
- dst (dict) – Data source document object.
- pointer_cls (Type[JsonPointer]) – JSON pointer class to use.
>>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]} >>> dst = {'baz': 'qux', 'numbers': [1, 4, 7]} >>> patch = make_patch(src, dst) >>> new = patch.apply(src) >>> new == dst True
-
jsonpatch.multidict(ordered_pairs)¶ Convert duplicate keys values to lists.