Applications Area Working Group P. Bryan, Ed.
Internet-Draft Salesforce.com
Intended status: Standards Track M. Nottingham, Ed.
Expires: July 24, 2013 Akamai
January 20, 2013
JSON Patchdraft-ietf-appsawg-json-patch-10
Abstract
JSON Patch defines a JSON document structure for expressing a
sequence of operations to apply to a JavaScript Object Notation
(JSON) document, suitable for use with the HTTP PATCH method. The
"application/json-patch" media type is used to identify such patch
documents.
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 July 24, 2013.
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
Bryan & Nottingham Expires July 24, 2013 [Page 1]

Internet-Draft JSON Patch January 20131. 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") for expressing a sequence of operations to apply to a
target JSON document, suitable for use with the HTTP PATCH method.
This format is also potentially useful in other cases where necessary
to make partial updates to a JSON document, or to a data structure
that has similar constraints (i.e., they can be serialised 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].
See Section 5 for information about handling errors.
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.
An example JSON Patch document, transferred in a HTTP PATCH request:
PATCH /my/data HTTP/1.1
Host: example.org
Content-Length: 326
Content-Type: application/json-patch
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 Expires July 24, 2013 [Page 3]

Internet-Draft JSON Patch January 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 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 is defined below.
Additionally, operation objects MUST have exactly one "path" member.
That member's value is a string containing a [JSON-Pointer] value
that references a location within the target document (the "target
location") where the operation is performed.
The meanings of other members of operation objects 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 the following function, 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 Expires July 24, 2013 [Page 4]

Internet-Draft JSON Patch January 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 [JSON-Pointer]), 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 behaviour 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 Expires July 24, 2013 [Page 5]

Internet-Draft JSON Patch January 20134.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 contain a "from" member, a string
containing a JSON Pointer value that references the location in the
target document to move the value from.
The "from" location MUST exist for the operation to be successful.
For example:
{ "op": "move", "from": "/a/b/c", "path": "/a/b/d" }
This operation is functionally identical to a "remove" operation on
the "from" location, followed immediately by an "add" operation at
the target location with the value that was just removed.
Bryan & Nottingham Expires July 24, 2013 [Page 6]

Internet-Draft JSON Patch January 2013
The "from" location MUST NOT be a proper prefix of the "path"
location; i.e., a location cannot be moved into one of its children.
4.5. copy
The "copy" operation copies the value at a specified location to the
target location.
The operation object MUST contain a "from" member, a string
containing a JSON Pointer value that references the location in the
target document to copy the value from.
The "from" location MUST exist for the operation to be successful.
For example:
{ "op": "copy", "from": "/a/b/c", "path": "/a/b/e" }
This operation is functionally identical to an "add" operation at the
target location using the value specified in the "from" member.
4.6. test
The "test" operation tests that a value at the target location is
equal to a specified value.
The operation object MUST contain a "value" member that conveys the
value to be compared to that at the target location.
The target location MUST be equal to the "value" value for the
operation to be considered successful.
Here, "equal" means that the value at the target location and that
conveyed by "value" are of the same JSON type, and considered equal
by the following rules for that type:
o strings: are considered equal if they contain the same number of
Unicode characters and their code points are position-wise equal.
o numbers: are considered equal if their values are numerically
equal.
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, using this list of
type-specific rules.
Bryan & Nottingham Expires July 24, 2013 [Page 7]

Internet-Draft JSON Patch January 2013
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.
For example:
{ "op": "test", "path": "/a/b/c", "value": "foo" }
5. Error Handling
If a normative requirement is violated by a JSON Patch document, or
if an operation is not successful, evaluation of the JSON Patch
document SHOULD terminate and application of the entire patch
document SHALL NOT be deemed successful.
See [RFC5789], Section 2.2 for considerations regarding handling
errors when JSON Patch is used with the HTTP PATCH method, including
suggested status codes to use to indicate various conditions.
Note that the HTTP PATCH method is atomic, as per [RFC5789].
Therefore, the following patch would result in no changes being made
to the document at all (because the "test" operation results in an
error).
[
{ "op": "replace", "path": "/a/b/c", "value": 42 },
{ "op": "test", "path": "/a/b/c", "value": "C" }
]
6. IANA Considerations
The Internet media type for a JSON Patch document is application/
json-patch.
Bryan & Nottingham Expires July 24, 2013 [Page 8]