rfc-6902

JavaScript Object Notation (JSON) Patch

P. Bryan (Editor), M. Nottingham (Editor)
date2013-04 streamIETF areaart wgappsawg statusPROPOSED STANDARD pages18 canonicalhttps://www.rfc-editor.org/rfc/rfc6902 doi10.17487/RFC6902 errataview
JSON Patch defines a JSON document structure for expressing a sequence of operations to apply to a JavaScript Object Notation (JSON) document; it is suitable for use with the HTTP PATCH method. The "application/json-patch+json" media type is used to identify such patch documents.

Extracted elements (22)

design-rationale §4.1

The "add" operation is designed to add to existing objects and arrays, so its target location will often not exist. The spec overrides the JSON Pointer error-handling algorithm to allow this, but the parent object or array must itself exist.

json

design-rationale §8

The structure of a JSON Patch document was influenced by the XML Patch document specification (RFC 5261), which similarly defines a sequence of patch operations applied to a target document.

json

design-rationale §4.6

The "test" operation's equality comparison is a logical comparison, not a serialization comparison: whitespace between array values and ordering of object member serialization are not significant.

json

interoperability-note §A.13

Duplicate "op" member names in an operation object (as shown in Appendix A.13) cannot be reliably handled because JSON's uniqueness requirement for object member names is only a SHOULD, and no standard error handling for duplicates exists.

json

interoperability-note §4

JSON object member ordering is not significant; therefore operation objects with the same members in different orders are equivalent (e.g., {"op":"add","path":"/a","value":"foo"} equals {"value":"foo","path":"/a","op":"add"}).

json

interoperability-note §5

When used with the HTTP PATCH method, JSON Patch inherits HTTP PATCH's atomicity requirement (RFC 5789 Section 2.2). A failed "test" operation causes the entire patch — including prior operations in the sequence — to be rolled back.

json, http

normative-requirement §4.1 MUST NOT

For the "add" operation targeting an array, the specified index MUST NOT be greater than the number of elements in the array. The "-" token appends to the end.

json

normative-requirement §4.4 MUST NOT

For the "move" operation, the "from" location MUST NOT be a proper prefix of the "path" location; a location cannot be moved into one of its children.

json

normative-requirement §5 SHOULD

If any normative requirement is violated or any operation is unsuccessful, evaluation of the JSON Patch document SHOULD terminate and the entire patch SHALL NOT be deemed successful (atomic all-or-nothing semantics).

json, http

normative-requirement §4 MUST

Members not explicitly defined for an operation MUST be ignored; the operation completes as if the undefined member did not appear.

json

normative-requirement §4 MUST

Operation objects MUST have exactly one "op" member whose value MUST be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors.

json

normative-requirement §4 MUST

Operation objects MUST have exactly one "path" member whose value is a JSON Pointer (RFC 6901) string referencing the target location within the document.

json

protocol-element §3

A JSON Patch document is a JSON array of operation objects. Each object represents a single operation to be applied sequentially to the target JSON document. Evaluation continues until all operations succeed or an error is encountered.

json

protocol-element §4.1

The "add" operation inserts a value at an array index, adds a new object member if absent, or replaces an existing member's value. The operation object MUST contain a "value" member specifying the value to add.

json

protocol-element §4.5

The "copy" operation copies the value at the "from" location to the target location. The operation object MUST contain a "from" member (a JSON Pointer string), and the "from" location MUST exist.

json

protocol-element §4.4

The "move" operation removes the value at the "from" location and adds it to the target location. The operation object MUST contain a "from" member (a JSON Pointer string), and the "from" location MUST exist.

json

protocol-element §4.2

The "remove" operation removes the value at the target location. The target location MUST exist; removing an array element shifts higher-index elements one position left.

json

protocol-element §4.3

The "replace" operation replaces the value at the target location with a new value specified in the required "value" member. The target location MUST exist and is functionally equivalent to remove followed by add.

json

protocol-element §4.6

The "test" operation tests that the value at the target location equals a specified "value" member. Equality is type-aware: strings compare code-point-by-code-point, numbers compare numerically, arrays and objects compare recursively, and literals compare by identity.

json

registry §6

This document registers the "application/json-patch+json" media type with IANA for identifying JSON Patch documents. File extension is ".json-patch"; encoding is binary; change controller is IETF.

json, registry

security-consideration §7

JSON Patch inherits the security considerations of JSON (RFC 4627) and JSON Pointer (RFC 6901). Additionally, some older browsers can be coerced into loading a JSON array as a script, exposing a JSON Patch document containing sensitive information via a Cross-Site Request Forgery (CSRF) attack.

json, security

security-consideration §7

Publishers concerned about CSRF exposure of JSON Patch documents (whose root is an array) are advised to avoid serving such documents via HTTP GET, since older browsers (estimated <1% market share at time of writing) can be coerced into fetching them cross-origin.

json, security, http