Standard RFC 6902¶
This module is the lowest-friction entry point when you want plain JSON Patch
behavior. It contains JsonPatch and the apply_patch convenience function.
jsonpatch.standard
Core patch application engine + public convenience wrappers.
This module is the “behavioral center” of the library: it defines copy semantics, error semantics, and the operational contract for applying a sequence of typed operations to a JSON document.
Copy & mutation semantics (single source of truth)¶
Operations are allowed to be mutative (i.e., they may modify the document object they receive). The engine controls whether those mutations affect the caller's original document:
-
inplace=False (default): deep-copy the input document first, then apply ops to the copy. The caller's original object is not modified.
-
inplace=True: apply ops directly to the input object (faster, avoids copy) but is NOT transactional. If an operation fails mid-patch, earlier operations may already have mutated the document (no rollback). This is a copy policy, not an object-identity guarantee: root-targeting operations (path "") may return a new root value.
Typed pointer semantics (why some failures are “intentional”)¶
Operation schemas frequently carry typed pointers: JSONPointer[T].
Typing is not only static; it is a runtime contract: - Pointer reads validate the resolved value against T. - This implies “type-gated” behavior for composite semantics like remove/replace: remove can fail if the current value exists but does not conform to T, because the operation is explicitly scoped to “what is removable”.
This library prefers explicitness: - widen T (e.g., JSONValue) to be permissive - or define a more specific op if you want stricter behavior
Error semantics¶
Expected patch failures (subclasses of PatchError) propagate unchanged.
Unexpected exceptions are wrapped with structured metadata (operation index + the full operation payload) so API layers can report actionable failures.
JsonPatch
¶
Bases: Sequence[OperationSchema]
A parsed JSON Patch document (RFC 6902-style) bound to a registry declaration.
JsonPatch is a convenience wrapper that:
- parses and validates an input patch document using a registry of
OperationSchemamodels, - stores the resulting typed
OperationSchemainstances, - applies them to JSON documents via the shared patch engine.
Notes
applydelegates to the core engine_apply_opsand follows the same copy and mutation semantics.inplace=False(default): the engine deep-copiesdocfirst; operations may mutate the copy.inplace=True: operations run against the provideddocobject (no rollback on failure). This is a copy policy, not an object-identity guarantee for the returned value.JsonPatchis immutable with respect to its operation list after construction, but the documents you apply it to may be mutated depending oninplace.
Source code in jsonpatchx/standard.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
ops
property
¶
The sequence of operations.
apply(doc, *, inplace=False)
¶
Apply this patch to doc and return the patched document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
JSONValue
|
The target JSON document. |
required |
inplace
|
bool
|
Copy policy. |
False
|
Return
patched: The patched JSON document.
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the input is not a mutable |
PatchError
|
Any patch-domain error raised by operations, including conflicts.
|
Source code in jsonpatchx/standard.py
from_string(text, *, registry=None)
classmethod
¶
Construct a JsonPatch from a JSON-formatted string.
JSON decoding follows last-write-wins just like json.loads()
If you want strict duplicate-key rejection, parse JSON yourself and pass the result to JsonPatch().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str | bytes | bytearray
|
JSON-formatted string/bytes/bytearray for a JSON Patch document. |
required |
registry
|
TypeForm[OperationSchema] | None
|
A union of concrete OperationSchemas used for parsing and
validation ( |
None
|
Source code in jsonpatchx/standard.py
apply_patch(doc, patch, *, registry=None, inplace=False)
¶
Apply a standard RFC 6902 JSON Patch document to doc.
This is a small convenience wrapper around JsonPatch using the standard registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
JSONValue
|
Target JSON document. |
required |
patch
|
Sequence[Mapping[str, JSONValue]]
|
Patch document as a sequence of operation mappings. |
required |
registry
|
TypeForm[OperationSchema] | None
|
A union of concrete OperationSchemas used for parsing and
validation ( |
None
|
inplace
|
bool
|
Copy policy. |
False
|
Returns:
| Type | Description |
|---|---|
JSONValue
|
The patched document. |