Applications Area Working Group P. Bryan, Ed.
Internet-Draft Salesforce.com
Intended status: Informational M. Nottingham, Ed.
Expires: March 9, 2013 September 5, 2012
JSON Patchdraft-ietf-appsawg-json-patch-03
Abstract
JSON Patch defines the media type "application/json-patch", a JSON
document structure for expressing a sequence of operations to apply
to a JSON document.
Status of this Memo
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 March 9, 2013.
Copyright Notice
Copyright (c) 2012 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 Expires March 9, 2013 [Page 1]

Internet-Draft JSON Patch September 20121. 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.
The JSON Patch media type "application/json-patch" is a JSON document
structure for expressing a sequence of operations to apply to a
target JSON document, suitable for use with the HTTP PATCH method.
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 contains a JSON array of objects. Each object
contains a single operation to apply to the target JSON document.
An example JSON Patch document:
[
{ "test": "/a/b/c", "value": "foo" },
{ "remove": "/a/b/c" },
{ "add": "/a/b/c", "value": [ "foo", "bar" ] },
{ "replace": "/a/b/c", "value": 42 },
{ "move": "/a/b/c", "to": "/a/b/d" },
{ "copy": "/a/b/c", "to": "/a/b/e" }
]
Evaluation of a JSON Patch document begins with 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 an error condition is encountered.
4. Operations
The operation to perform is expressed in a member of the operation
object. The name of the operation member is one of: "add", "remove",
"replace", "move", "copy" or "test".
Bryan & Nottingham Expires March 9, 2013 [Page 3]

Internet-Draft JSON Patch September 2012
The member value is a string containing a [JSON-Pointer] value that
references the location within the target document to perform the
operation. It is an error condition if an operation object contains
no recognized operation member or more than one operation member.
4.1. add
The "add" operation adds a new value at the specified location in the
target document. The location must reference one of: the root of the
target document, a member to add to an existing object, or an element
to add to an existing array. The operation object contains a "value"
member that specifies the value to be added.
Example:
{ "add": "/a/b/c", "value": [ "foo", "bar" ] }
If the location references the root of the target document or a
member of an existing object, it is an error condition if a value at
the specified location already exists.
If the location references an element of an existing array, any
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 array.
Note that this operation will, in common use, contain a JSON Pointer
that does not resolve to an existing value in the target document.
As such, the pointer's error handling algorithm is invoked. This
specification defines the error handling algorithm for "add" pointers
to explicitly ignore the error and perform the operation as
specified.
4.2. remove
The "remove" operation removes the value at the specified location in
the target document.
Example:
{ "remove": "/a/b/c" }
If removing an element from an array, any elements above the
specified index are shifted one position to the left.
It is an error condition if a value at the specified location does
not exist.
Bryan & Nottingham Expires March 9, 2013 [Page 4]

Internet-Draft JSON Patch September 20124.3. replace
The "replace" operation replaces the value at the specified location
in the target document with a new value. The operation object
contains a "value" member that specifies the replacement value.
Example:
{ "replace": "/a/b/c", "value": 42 }
This operation is functionally identical to expressing a "remove"
operation for a value, followed immediately by an "add" operation at
the same location with the replacement value.
It is an error condition if a value at the specified location does
not exist.
4.4. move
The "move" operation removes the value at one location and adds it to
another location in the target document.
The operation object contains a "to" member, a string containing a
JSON Pointer value that references the location in the target
document to add the value to. This location must reference one of:
the member to add to an existing object, or an element to add to an
existing array.
Example:
{ "move": "/a/b/c", "to": "/a/b/d" }
This operation is functionally identical to expressing a "remove"
operation, followed immediately by an "add" operation at the new
location with the value that was just removed. Moving a value to its
current location can be safely ignored.
If the location in the "to" member references a member of an existing
object in the target document, it is an error condition if a value at
the specified location already exists (unless "move" and "to" specify
the same object, which has no effect).
If the location in the "to" member references an element of an
existing array, any 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 array.
Bryan & Nottingham Expires March 9, 2013 [Page 5]

Internet-Draft JSON Patch September 20124.5. copy
The "copy" operation copies the value at one location to another
location in the target document.
The operation object contains a "to" member, a string containing a
JSON Pointer value that references the location in the target
document to add the value to. This location must reference one of:
the member to add to an existing object, or an element to add to an
existing array.
Example:
{ "copy": "/a/b/c", "to": "/a/b/e" }
If the location in the "to" member references a member of an existing
object in the target document, it is an error condition if a value at
the specified location already exists.
If the location in the "to" member references an element of an
existing array, any 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 array.
4.6. test
The "test" operation tests that a value at the specified location in
the target document is equal to a specified value. The operation
object contains a "value" member that specifies the value to test
for.
Here, "equal" means that the target and specified values are of the
same JSON type, and considered equal by the following rules for that
type:
o strings: are considered equal if, after unescaping any sequence(s)
in both strings starting with a reverse solidus, they contain the
same number of Unicode characters and their code points are
position-wise equal.
o numbers: are considered equal if subtracting one from the other
results in 0.
o arrays: are considered equal if they contain the same number of
values, and each value can be considered equal to the value at the
corresponding position in the other array.
Bryan & Nottingham Expires March 9, 2013 [Page 6]

Internet-Draft JSON Patch September 2012
o objects: are considered equal if they contain the same number of
members, and each member can be considered equal to a member in
the other object, by comparing their keys as strings, and values
using this list of type-specific rules.
o literals (false, true and null): are considered equal if they are
the same.
Note that this is a logical comparison; e.g., whitespace between the
member values of an array is not significant.
Also, note that ordering of the serialisation of object members is
not significant.
Example:
{ "test": "/a/b/c", "value": "foo" }
It is an error condition if the value at the specified location is
not equal to the specified value.
5. Error Handling
If an error condition occurs, evaluation of the JSON Patch document
SHOULD terminate and application of the entire patch document SHALL
NOT be deemed successful. Note that as per [RFC5789], PATCH is
atomic.
6. IANA Considerations
The Internet media type for a JSON Patch document is application/
json-patch.
Type name: application
Subtype name: json-patch
Required parameters: none
Optional parameters: none
Encoding considerations: binary
Bryan & Nottingham Expires March 9, 2013 [Page 7]