Internet Engineering Task Force | P. Bryan, Ed. |
Internet-Draft | ForgeRock US, Inc. |
Intended status: Informational | October 23, 2011 |
Expires: April 25, 2012 |
JSON Patch
draft-pbryan-json-patch-02
JSON Patch defines the media type "application/json-patch", a JSON document structure for expressing a sequence of partial modifications to a JSON document.
This Internet-Draft is submitted in full conformance with the provisions of BCP 78 and BCP 79.
Internet-Drafts are working documents of the Internet Engineering Task Force (IETF). Note that other groups may also distribute working documents as Internet-Drafts. The list of current Internet- Drafts is at http://datatracker.ietf.org/drafts/current/.
Internet-Drafts are draft documents valid for a maximum of six months and may be updated, replaced, or obsoleted by other documents at any time. It is inappropriate to use Internet-Drafts as reference material or to cite them other than as "work in progress."
This Internet-Draft will expire on April 25, 2012.
Copyright (c) 2011 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.
JavaScript Object Notation (JSON) [RFC4627] is a common format for the exchange and storage of structured data. HTTP PATCH [RFC5789] extends HTTP [RFC2616] with a method to perform partial modifications to resources.
The JSON Patch media type "application/json-patch" is a JSON document structure for expressing a sequence of partial modifications to a JSON document, suitable for use with the HTTP PATCH method.
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].
A JSON Patch document contains a JSON array of objects. Each object contains an operation to apply to the target JSON document.
A sample JSON Patch document:
[ { "remove": "/a/b/c" }, { "add": "/a/b/c", "value": "foo" }, { "replace": "/a/b/c", "value": "bar" } ]
Evaluation of a JSON Patch document begins with a target JSON document to modify. 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 modified document becomes the target for the next operation. The process repeats until all operations are successfully applied.
The operation to perform is expressed in the name of a member in the operation object, with that member's value holding a string containing the [JSON Pointer], which references the value to modify. It is an error condition if an operation object contains more than one operation member.
The "add" operation adds a new value into an object or array. The value to be added is specified in the operation object's "value" member.
If adding to an object, it is an error condition if the member to be added in the object already exists.
If adding to an array, all elements at or above the specified index are shifted one position to the right. It is an error condition if the specified index is greater than the number of elements in the existing array.
The "remove" operation removes a value from an object or array.
If removing an element from an array, all elements above the specified index are shifted one position to the left.
It is an error condition if the value to be removed does not exist.
The "replace" operation replaces an existing value with a new value. This operation is equivalent to expressing a "remove" operation for a value, followed immediately by an "add" operation for the same value. The value to be replaced is specified in the operation object's "value" member.
It is an error condition if the value to be replaced does not exist.
In the event of an error condition, evaluation of the JSON Patch document terminates and modification of the target document fails to complete.
The structure of a JSON Patch document was informed by the XML Patch document [RFC5261] specification.
The Internet media type for a JSON Patch document is application/json-patch.
This specification has the same security considerations as JSON [RFC4627] and JSON Pointer [JSON Pointer].
[RFC2119] | Bradner, S., "Key words for use in RFCs to Indicate Requirement Levels", BCP 14, RFC 2119, March 1997. |
[RFC4627] | Crockford, D., "The application/json Media Type for JavaScript Object Notation (JSON)", RFC 4627, July 2006. |
[JSON Pointer] | Bryan, P. and K. Zyp, "JSON Pointer", October 2011. |
[RFC2616] | Fielding, R., Gettys, J., Mogul, J., Frystyk, H., Masinter, L., Leach, P. and T. Berners-Lee, "Hypertext Transfer Protocol -- HTTP/1.1", RFC 2616, June 1999. |
[RFC5261] | Urpalainen, J., "An Extensible Markup Language (XML) Patch Operations Framework Utilizing XML Path Language (XPath) Selectors", RFC 5261, September 2008. |
[RFC5789] | Dusseault, L. and J. Snell, "PATCH Method for HTTP", RFC 5789, March 2010. |
An example target JSON document:
{ "foo": "bar" }
A JSON Patch document:
[ { "add": "/baz", "value": "qux" } ]
The resulting JSON document:
{ "baz": "qux", "foo": "bar" }
An example target JSON document:
{ "foo": [ "bar", "baz" ] }
A JSON Patch document:
[ { "add": "/foo/1", "value": "qux" } ]
The resulting JSON document:
{ "foo": [ "bar", "qux", "baz" ] }
An example target JSON document:
{ "baz": "qux", "foo": "bar" }
A JSON Patch document:
[ { "remove": "/baz" } ]
The resulting JSON document:
{ "foo": "bar" }
An example target JSON document:
{ "foo": [ "bar", "qux", "baz" ] }
A JSON Patch document:
[ { "remove": "/foo/1" } ]
The resulting JSON document:
{ "foo": ["bar", "baz"] }
An example target JSON document:
{ "baz": "qux", "foo": "bar" }
A JSON Patch document:
[ { "replace": "/baz", "value": "boo" } ]
The resulting JSON document:
{ "baz": "boo", "foo": "bar" }