RFC 6902 - JavaScript Object Notation (JSON) Patch Light Dark Auto RFC 6902 Proposed Standard Title JavaScript Object Notation (JSON) Patch Document Document type RFC - Proposed Standard April 2013 View errata Report errata Was draft-ietf-appsawg-json-patch (appsawg WG) Select version 00 01 02 03 04 05 06 07 08 09 10 RFC 6902 Compare versions RFC 6902 draft-ietf-appsawg-json-patch-10 draft-ietf-appsawg-json-patch-09 draft-ietf-appsawg-json-patch-08 draft-ietf-appsawg-json-patch-07 draft-ietf-appsawg-json-patch-06 draft-ietf-appsawg-json-patch-05 draft-ietf-appsawg-json-patch-04 draft-ietf-appsawg-json-patch-03 draft-ietf-appsawg-json-patch-02 draft-ietf-appsawg-json-patch-01 draft-ietf-appsawg-json-patch-00 RFC 6902 draft-ietf-appsawg-json-patch-10 draft-ietf-appsawg-json-patch-09 draft-ietf-appsawg-json-patch-08 draft-ietf-appsawg-json-patch-07 draft-ietf-appsawg-json-patch-06 draft-ietf-appsawg-json-patch-05 draft-ietf-appsawg-json-patch-04 draft-ietf-appsawg-json-patch-03 draft-ietf-appsawg-json-patch-02 draft-ietf-appsawg-json-patch-01 draft-ietf-appsawg-json-patch-00 Side-by-side Inline Authors P. Bryan , M. Nottingham Email authors RFC stream Other formats txt html bibtex Additional resources Mailing list discussion Report a bug Internet Engineering Task Force (IETF) P. Bryan, Ed. Request for Comments: 6902 Salesforce.com Category: Standards Track M. Nottingham, Ed. ISSN: 2070-1721 Akamai April 2013 JavaScript Object Notation (JSON) Patch Abstract 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. Status of This Memo This is an Internet Standards Track document. This document is a product of the Internet Engineering Task Force (IETF). It represents the consensus of the IETF community. It has received public review and has been approved for publication by the Internet Engineering Steering Group (IESG). Further information on Internet Standards is available in Section 2 of RFC 5741. Information about the current status of this document, any errata, and how to provide feedback on it may be obtained at http://www.rfc-editor.org/info/rfc6902. Copyright Notice Copyright (c) 2013 IETF Trust and the persons identified as the document authors. All rights reserved. This document is subject to BCP 78 and the IETF Trust's Legal Provisions Relating to IETF Documents (http://trustee.ietf.org/license-info) in effect on the date of publication of this document. Please review these documents carefully, as they describe your rights and restrictions with respect to this document. Code Components extracted from this document must include Simplified BSD License text as described in Section 4.e of the Trust Legal Provisions and are provided without warranty as described in the Simplified BSD License. Bryan & Nottingham Standards Track [Page 1] RFC 6902 JSON Patch April 2013 Table of Contents 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 3 2. Conventions . . . . . . . . . . . . . . . . . . . . . . . . . 3 3. Document Structure . . . . . . . . . . . . . . . . . . . . . . 3 4. Operations . . . . . . . . . . . . . . . . . . . . . . . . . . 4 4.1. add . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 4.2. remove . . . . . . . . . . . . . . . . . . . . . . . . . . 6 4.3. replace . . . . . . . . . . . . . . . . . . . . . . . . . 6 4.4. move . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 4.5. copy . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 4.6. test . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 5. Error Handling . . . . . . . . . . . . . . . . . . . . . . . . 8 6. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 9 7. Security Considerations . . . . . . . . . . . . . . . . . . . 10 8. Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . 10 9. References . . . . . . . . . . . . . . . . . . . . . . . . . . 10 9.1. Normative References . . . . . . . . . . . . . . . . . . . 10 9.2. Informative References . . . . . . . . . . . . . . . . . . 10 Appendix A. Examples . . . . . . . . . . . . . . . . . . . . . . 12 A.1. Adding an Object Member . . . . . . . . . . . . . . . . . 12 A.2. Adding an Array Element . . . . . . . . . . . . . . . . . 12 A.3. Removing an Object Member . . . . . . . . . . . . . . . . 12 A.4. Removing an Array Element . . . . . . . . . . . . . . . . 13 A.5. Replacing a Value . . . . . . . . . . . . . . . . . . . . 13 A.6. Moving a Value . . . . . . . . . . . . . . . . . . . . . . 14 A.7. Moving an Array Element . . . . . . . . . . . . . . . . . 14 A.8. Testing a Value: Success . . . . . . . . . . . . . . . . . 15 A.9. Testing a Value: Error . . . . . . . . . . . . . . . . . . 15 A.10. Adding a Nested Member Object . . . . . . . . . . . . . . 15 A.11. Ignoring Unrecognized Elements . . . . . . . . . . . . . . 16 A.12. Adding to a Nonexistent Target . . . . . . . . . . . . . . 16 A.13. Invalid JSON Patch Document . . . . . . . . . . . . . . . 17 A.14. ~ Escape Ordering . . . . . . . . . . . . . . . . . . . . 17 A.15. Comparing Strings and Numbers . . . . . . . . . . . . . . 17 A.16. Adding an Array Value . . . . . . . . . . . . . . . . . . 18 Bryan & Nottingham Standards Track [Page 2] RFC 6902 JSON Patch April 2013 1. Introduction JavaScript Object Notation (JSON) [RFC4627] is a common format for the exchange and storage of structured data. HTTP PATCH [RFC5789] extends the Hypertext Transfer Protocol (HTTP) [RFC2616] with a method to perform partial modifications to resources. JSON Patch is a format (identified by the media type "application/ json-patch+json") for expressing a sequence of operations to apply to a target JSON document; it is suitable for use with the HTTP PATCH method. This format is also potentially useful in other cases in which it is necessary to make partial updates to a JSON document or to a data structure that has similar constraints (i.e., they can be serialized as an object or an array using the JSON grammar). 2. Conventions The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119 [RFC2119]. 3. Document Structure A JSON Patch document is a JSON [RFC4627] document that represents an array of objects. Each object represents a single operation to be applied to the target JSON document. The following is an example JSON Patch document, transferred in an HTTP PATCH request: PATCH /my/data HTTP/1.1 Host: example.org Content-Length: 326 Content-Type: application/json-patch+json If-Match: "abc123" [ { "op": "test", "path": "/a/b/c", "value": "foo" }, { "op": "remove", "path": "/a/b/c" }, { "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] }, { "op": "replace", "path": "/a/b/c", "value": 42 }, { "op": "move", "from": "/a/b/c", "path": "/a/b/d" }, { "op": "copy", "from": "/a/b/d", "path": "/a/b/e" } ] Bryan & Nottingham Standards Track [Page 3] RFC 6902 JSON Patch April 2013 Evaluation of a JSON Patch document begins against a target JSON document. Operations are applied sequentially in the order they appear in the array. Each operation in the sequence is applied to the target document; the resulting document becomes the target of the next operation. Evaluation continues until all operations are successfully applied or until an error condition is encountered. 4. Operations Operation objects MUST have exactly one "op" member, whose value indicates the operation to perform. Its value MUST be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors. The semantics of each object is defined below. Additionally, operation objects MUST have exactly one "path" member. That member's value is a string containing a JSON-Pointer value [RFC6901] that references a location within the target document (the "target location") where the operation is performed. The meanings of other operation object members are defined by operation (see the subsections below). Members that are not explicitly defined for the operation in question MUST be ignored (i.e., the operation will complete as if the undefined member did not appear in the object). Note that the ordering of members in JSON objects is not significant; therefore, the following operation objects are equivalent: { "op": "add", "path": "/a/b/c", "value": "foo" } { "path": "/a/b/c", "op": "add", "value": "foo" } { "value": "foo", "path": "/a/b/c", "op": "add" } Operations are applied to the data structures represented by a JSON document, i.e., after any unescaping (see [RFC4627], Section 2.5) takes place. 4.1. add The "add" operation performs one of the following functions, depending upon what the target location references: o If the target location specifies an array index, a new value is inserted into the array at the specified index. o If the target location specifies an object member that does not already exist, a new member is added to the object. Bryan & Nottingham Standards Track [Page 4] RFC 6902 JSON Patch April 2013 o If the target location specifies an object member that does exist, that member's value is replaced. The operation object MUST contain a "value" member whose content specifies the value to be added. For example: { "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] } When the operation is applied, the target location MUST reference one of: o The root of the target document - whereupon the specified value becomes the entire content of the target document. o A member to add to an existing object - whereupon the supplied value is added to that object at the indicated location. If the member already exists, it is replaced by the specified value. o An element to add to an existing array - whereupon the supplied value is added to the array at the indicated location. Any elements at or above the specified index are shifted one position to the right. The specified index MUST NOT be greater than the number of elements in the array. If the "-" character is used to index the end of the array (see [RFC6901]), this has the effect of appending the value to the array. Because this operation is designed to add to existing objects and arrays, its target location will often not exist. Although the pointer's error handling algorithm will thus be invoked, this specification defines the error handling behavior for "add" pointers to ignore that error and add the value as specified. However, the object itself or an array containing it does need to exist, and it remains an error for that not to be the case. For example, an "add" with a target location of "/a/b" starting with this document: { "a": { "foo": 1 } } is not an error, because "a" exists, and "b" will be added to its value. It is an error in this document: { "q": { "bar": 2 } } because "a" does not exist. Bryan & Nottingham Standards Track [Page 5] RFC 6902 JSON Patch April 2013 4.2. remove The "remove" operation removes the value at the target location. The target location MUST exist for the operation to be successful. For example: { "op": "remove", "path": "/a/b/c" } If removing an element from an array, any elements above the specified index are shifted one position to the left. 4.3. replace The "replace" operation replaces the value at the target location with a new value. The operation object MUST contain a "value" member whose content specifies the replacement value. The target location MUST exist for the operation to be successful. For example: { "op": "replace", "path": "/a/b/c", "value": 42 } This operation is functionally identical to a "remove" operation for a value, followed immediately by an "add" operation at the same location with the replacement value. 4.4. move The "move" operation removes the value at a specified location and adds it to the target location. The operation object MUST cont…