6902
PROPOSED STANDARD

JavaScript Object Notation (JSON) Patch

Authors: P. Bryan, M. Nottingham
Date: April 2013
Area: app
Working Group: appsawg
Stream: IETF

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.

RFC 6902: JavaScript Object Notation (JSON) Patch [RFC Home] [TEXT|PDF|HTML] [Tracker] [IPR] [Errata] [Info page]

PROPOSED STANDARD
Errata Exist
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


                <span class="h1">JavaScript Object Notation (JSON) Patch</span>

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 <a href="./rfc5741#section-2">Section 2 of RFC 5741</a>.

   Information about the current status of this document, any errata,
   and how to provide feedback on it may be obtained at
   <a href="https://www.rfc-editor.org/info/rfc6902">http://www.rfc-editor.org/info/rfc6902</a>.

Copyright Notice

   Copyright (c) 2013 IETF Trust and the persons identified as the
   document authors.  All rights reserved.

   This document is subject to <a href="https://www.rfc-editor.org/bcp/bcp78">BCP 78</a> and the IETF Trust's Legal
   Provisions Relating to IETF Documents
   (<a href="http://trustee.ietf.org/license-info">http://trustee.ietf.org/license-info</a>) 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.






<span class="grey">Bryan & Nottingham           Standards Track                    [Page 1]</span>

<span id="page-2" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


Table of Contents

   <a href="#section-1">1</a>.  Introduction . . . . . . . . . . . . . . . . . . . . . . . . .  <a href="#page-3">3</a>
   <a href="#section-2">2</a>.  Conventions  . . . . . . . . . . . . . . . . . . . . . . . . .  <a href="#page-3">3</a>
   <a href="#section-3">3</a>.  Document Structure . . . . . . . . . . . . . . . . . . . . . .  <a href="#page-3">3</a>
   <a href="#section-4">4</a>.  Operations . . . . . . . . . . . . . . . . . . . . . . . . . .  <a href="#page-4">4</a>
     <a href="#section-4.1">4.1</a>.  add  . . . . . . . . . . . . . . . . . . . . . . . . . . .  <a href="#page-4">4</a>
     <a href="#section-4.2">4.2</a>.  remove . . . . . . . . . . . . . . . . . . . . . . . . . .  <a href="#page-6">6</a>
     <a href="#section-4.3">4.3</a>.  replace  . . . . . . . . . . . . . . . . . . . . . . . . .  <a href="#page-6">6</a>
     <a href="#section-4.4">4.4</a>.  move . . . . . . . . . . . . . . . . . . . . . . . . . . .  <a href="#page-6">6</a>
     <a href="#section-4.5">4.5</a>.  copy . . . . . . . . . . . . . . . . . . . . . . . . . . .  <a href="#page-7">7</a>
     <a href="#section-4.6">4.6</a>.  test . . . . . . . . . . . . . . . . . . . . . . . . . . .  <a href="#page-7">7</a>
   <a href="#section-5">5</a>.  Error Handling . . . . . . . . . . . . . . . . . . . . . . . .  <a href="#page-8">8</a>
   <a href="#section-6">6</a>.  IANA Considerations  . . . . . . . . . . . . . . . . . . . . .  <a href="#page-9">9</a>
   <a href="#section-7">7</a>.  Security Considerations  . . . . . . . . . . . . . . . . . . . <a href="#page-10">10</a>
   <a href="#section-8">8</a>.  Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . <a href="#page-10">10</a>
   <a href="#section-9">9</a>.  References . . . . . . . . . . . . . . . . . . . . . . . . . . <a href="#page-10">10</a>
     <a href="#section-9.1">9.1</a>.  Normative References . . . . . . . . . . . . . . . . . . . <a href="#page-10">10</a>
     <a href="#section-9.2">9.2</a>.  Informative References . . . . . . . . . . . . . . . . . . <a href="#page-10">10</a>
   <a href="#appendix-A">Appendix A</a>.  Examples  . . . . . . . . . . . . . . . . . . . . . . <a href="#page-12">12</a>
     <a href="#appendix-A.1">A.1</a>.  Adding an Object Member  . . . . . . . . . . . . . . . . . <a href="#page-12">12</a>
     <a href="#appendix-A.2">A.2</a>.  Adding an Array Element  . . . . . . . . . . . . . . . . . <a href="#page-12">12</a>
     <a href="#appendix-A.3">A.3</a>.  Removing an Object Member  . . . . . . . . . . . . . . . . <a href="#page-12">12</a>
     <a href="#appendix-A.4">A.4</a>.  Removing an Array Element  . . . . . . . . . . . . . . . . <a href="#page-13">13</a>
     <a href="#appendix-A.5">A.5</a>.  Replacing a Value  . . . . . . . . . . . . . . . . . . . . <a href="#page-13">13</a>
     <a href="#appendix-A.6">A.6</a>.  Moving a Value . . . . . . . . . . . . . . . . . . . . . . <a href="#page-14">14</a>
     <a href="#appendix-A.7">A.7</a>.  Moving an Array Element  . . . . . . . . . . . . . . . . . <a href="#page-14">14</a>
     <a href="#appendix-A.8">A.8</a>.  Testing a Value: Success . . . . . . . . . . . . . . . . . <a href="#page-15">15</a>
     <a href="#appendix-A.9">A.9</a>.  Testing a Value: Error . . . . . . . . . . . . . . . . . . <a href="#page-15">15</a>
     <a href="#appendix-A.10">A.10</a>. Adding a Nested Member Object  . . . . . . . . . . . . . . <a href="#page-15">15</a>
     <a href="#appendix-A.11">A.11</a>. Ignoring Unrecognized Elements . . . . . . . . . . . . . . <a href="#page-16">16</a>
     <a href="#appendix-A.12">A.12</a>. Adding to a Nonexistent Target . . . . . . . . . . . . . . <a href="#page-16">16</a>
     <a href="#appendix-A.13">A.13</a>. Invalid JSON Patch Document  . . . . . . . . . . . . . . . <a href="#page-17">17</a>
     <a href="#appendix-A.14">A.14</a>. ~ Escape Ordering  . . . . . . . . . . . . . . . . . . . . <a href="#page-17">17</a>
     <a href="#appendix-A.15">A.15</a>. Comparing Strings and Numbers  . . . . . . . . . . . . . . <a href="#page-17">17</a>
     <a href="#appendix-A.16">A.16</a>. Adding an Array Value  . . . . . . . . . . . . . . . . . . <a href="#page-18">18</a>















<span class="grey">Bryan & Nottingham           Standards Track                    [Page 2]</span>

<span id="page-3" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


<span class="h2"><a class="selflink" id="section-1" href="#section-1">1</a>.  Introduction</span>

   JavaScript Object Notation (JSON) [<a href="./rfc4627" title=""The application/json Media Type for JavaScript Object Notation (JSON)"">RFC4627</a>] is a common format for
   the exchange and storage of structured data.  HTTP PATCH [<a href="./rfc5789" title=""PATCH Method for HTTP"">RFC5789</a>]
   extends the Hypertext Transfer Protocol (HTTP) [<a href="./rfc2616" title=""Hypertext Transfer Protocol -- HTTP/1.1"">RFC2616</a>] 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).

<span class="h2"><a class="selflink" id="section-2" href="#section-2">2</a>.  Conventions</span>

   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 <a href="./rfc2119">RFC 2119</a> [<a href="./rfc2119" title=""Key words for use in RFCs to Indicate Requirement Levels"">RFC2119</a>].

<span class="h2"><a class="selflink" id="section-3" href="#section-3">3</a>.  Document Structure</span>

   A JSON Patch document is a JSON [<a href="./rfc4627" title=""The application/json Media Type for JavaScript Object Notation (JSON)"">RFC4627</a>] 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" }
   ]





<span class="grey">Bryan & Nottingham           Standards Track                    [Page 3]</span>

<span id="page-4" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


   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.

<span class="h2"><a class="selflink" id="section-4" href="#section-4">4</a>.  Operations</span>

   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
   [<a href="./rfc6901" title=""JavaScript Object Notation (JSON) Pointer"">RFC6901</a>] 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 <a href="./rfc4627#section-2.5">[RFC4627], Section 2.5</a>)
   takes place.

<span class="h3"><a class="selflink" id="section-4.1" href="#section-4.1">4.1</a>.  add</span>

   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.





<span class="grey">Bryan & Nottingham           Standards Track                    [Page 4]</span>

<span id="page-5" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


   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 [<a href="./rfc6901" title=""JavaScript Object Notation (JSON) Pointer"">RFC6901</a>]), 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.




<span class="grey">Bryan & Nottingham           Standards Track                    [Page 5]</span>

<span id="page-6" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


<span class="h3"><a class="selflink" id="section-4.2" href="#section-4.2">4.2</a>.  remove</span>

   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.

<span class="h3"><a class="selflink" id="section-4.3" href="#section-4.3">4.3</a>.  replace</span>

   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.

<span class="h3"><a class="selflink" id="section-4.4" href="#section-4.4">4.4</a>.  move</span>

   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, which is 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.




<span class="grey">Bryan & Nottingham           Standards Track                    [Page 6]</span>

<span id="page-7" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


   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.

<span class="h3"><a class="selflink" id="section-4.5" href="#section-4.5">4.5</a>.  copy</span>

   The "copy" operation copies the value at a specified location to the
   target location.

   The operation object MUST contain a "from" member, which is 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.

<span class="h3"><a class="selflink" id="section-4.6" href="#section-4.6">4.6</a>.  test</span>

   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 the target location's value.

   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 the
   value conveyed by "value" are of the same JSON type, and that they
   are 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 byte-by-byte 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 if each value can be considered equal to the value at
      the corresponding position in the other array, using this list of
      type-specific rules.





<span class="grey">Bryan & Nottingham           Standards Track                    [Page 7]</span>

<span id="page-8" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


   o  objects: are considered equal if they contain the same number of
      members, and if each member can be considered equal to a member in
      the other object, by comparing their keys (as strings) and their
      values (using this list of type-specific rules).

   o  literals (false, true, and null): are considered equal if they are
      the same.

   Note that the comparison that is done is a logical comparison; e.g.,
   whitespace between the member values of an array is not significant.

   Also, note that ordering of the serialization of object members is
   not significant.

   For example:

   { "op": "test", "path": "/a/b/c", "value": "foo" }

<span class="h2"><a class="selflink" id="section-5" href="#section-5">5</a>.  Error Handling</span>

   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 <a href="./rfc5789#section-2.2">[RFC5789], Section 2.2</a> 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 [<a href="./rfc5789" title=""PATCH Method for HTTP"">RFC5789</a>].
   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" }
   ]













<span class="grey">Bryan & Nottingham           Standards Track                    [Page 8]</span>

<span id="page-9" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


<span class="h2"><a class="selflink" id="section-6" href="#section-6">6</a>.  IANA Considerations</span>

   The Internet media type for a JSON Patch document is application/
   json-patch+json.

   Type name:  application

   Subtype name:  json-patch+json

   Required parameters:  none

   Optional parameters:   none

   Encoding considerations:  binary

   Security considerations:
      See Security Considerations in <a href="#section-7">Section 7</a>.

   Interoperability considerations:  N/A

   Published specification:
      <a href="./rfc6902">RFC 6902</a>

   Applications that use this media type:
      Applications that manipulate JSON documents.

   Additional information:

      Magic number(s):  N/A

      File extension(s):  .json-patch

      Macintosh file type code(s):  TEXT

   Person & email address to contact for further information:
      Paul C. Bryan <pbryan@anode.ca>

   Intended usage:  COMMON

   Restrictions on usage:  none

   Author:  Paul C. Bryan <pbryan@anode.ca>

   Change controller:  IETF







<span class="grey">Bryan & Nottingham           Standards Track                    [Page 9]</span>

<span id="page-10" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


<span class="h2"><a class="selflink" id="section-7" href="#section-7">7</a>.  Security Considerations</span>

   This specification has the same security considerations as JSON
   [<a href="./rfc4627" title=""The application/json Media Type for JavaScript Object Notation (JSON)"">RFC4627</a>] and JSON-Pointer [<a href="./rfc6901" title=""JavaScript Object Notation (JSON) Pointer"">RFC6901</a>].

   A few older Web browsers can be coerced into loading an arbitrary
   JSON document whose root is an array, leading to a situation in which
   a JSON Patch document containing sensitive information could be
   exposed to attackers, even if access is authenticated.  This is known
   as a Cross-Site Request Forgery (CSRF) attack [<a href="#ref-CSRF" title=""Robust Defenses for Cross-Site Request Forgery"">CSRF</a>].

   However, such browsers are not widely used (at the time of writing,
   it is estimated that they are used in less than 1% of the market).
   Publishers who are nevertheless concerned about this attack are
   advised to avoid making such documents available with HTTP GET.

<span class="h2"><a class="selflink" id="section-8" href="#section-8">8</a>.  Acknowledgements</span>

   The following individuals contributed ideas, feedback and wording to
   this specification:

      Mike Acar, Mike Amundsen, Cyrus Daboo, Paul Davis, Stefan Koegl,
      Murray S. Kucherawy, Dean Landolt, Randall Leeds, James Manger,
      Julian Reschke, James Snell, Eli Stevens, and Henry S. Thompson.

   The structure of a JSON Patch document was influenced by the XML
   Patch document specification [<a href="./rfc5261" title=""An Extensible Markup Language (XML) Patch Operations Framework Utilizing XML Path Language (XPath) Selectors"">RFC5261</a>].

<span class="h2"><a class="selflink" id="section-9" href="#section-9">9</a>.  References</span>

<span class="h3"><a class="selflink" id="section-9.1" href="#section-9.1">9.1</a>.  Normative References</span>

   [<a id="ref-RFC2119">RFC2119</a>]  Bradner, S., "Key words for use in RFCs to Indicate
              Requirement Levels", <a href="https://www.rfc-editor.org/bcp/bcp14">BCP 14</a>, <a href="./rfc2119">RFC 2119</a>, March 1997.

   [<a id="ref-RFC4627">RFC4627</a>]  Crockford, D., "The application/json Media Type for
              JavaScript Object Notation (JSON)", <a href="./rfc4627">RFC 4627</a>, July 2006.

   [<a id="ref-RFC6901">RFC6901</a>]  Bryan, P., Ed., Zyp, K., and M. Nottingham, Ed.,
              "JavaScript Object Notation (JSON) Pointer", <a href="./rfc6901">RFC 6901</a>,
              April 2013.

<span class="h3"><a class="selflink" id="section-9.2" href="#section-9.2">9.2</a>.  Informative References</span>

   [<a id="ref-CSRF">CSRF</a>]     Barth, A., Jackson, C., and J. Mitchell, "Robust Defenses
              for Cross-Site Request Forgery", ACM Conference
              on Computer and Communications Security, October 2008,
              <<a href="http://seclab.stanford.edu/websec/csrf/csrf.pdf">http://seclab.stanford.edu/websec/csrf/csrf.pdf</a>>.



<span class="grey">Bryan & Nottingham           Standards Track                   [Page 10]</span>

<span id="page-11" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


   [<a id="ref-RFC2616">RFC2616</a>]  Fielding, R., Gettys, J., Mogul, J., Frystyk, H.,
              Masinter, L., Leach, P., and T. Berners-Lee, "Hypertext
              Transfer Protocol -- HTTP/1.1", <a href="./rfc2616">RFC 2616</a>, June 1999.

   [<a id="ref-RFC5261">RFC5261</a>]  Urpalainen, J., "An Extensible Markup Language (XML) Patch
              Operations Framework Utilizing XML Path Language (XPath)
              Selectors", <a href="./rfc5261">RFC 5261</a>, September 2008.

   [<a id="ref-RFC5789">RFC5789</a>]  Dusseault, L. and J. Snell, "PATCH Method for HTTP",
              <a href="./rfc5789">RFC 5789</a>, March 2010.









































<span class="grey">Bryan & Nottingham           Standards Track                   [Page 11]</span>

<span id="page-12" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


<span class="h2"><a class="selflink" id="appendix-A" href="#appendix-A">Appendix A</a>.  Examples</span>

<span class="h3"><a class="selflink" id="appendix-A.1" href="#appendix-A.1">A.1</a>.  Adding an Object Member</span>

   An example target JSON document:

   { "foo": "bar"}

   A JSON Patch document:

   [
     { "op": "add", "path": "/baz", "value": "qux" }
   ]

   The resulting JSON document:

   {
     "baz": "qux",
     "foo": "bar"
   }

<span class="h3"><a class="selflink" id="appendix-A.2" href="#appendix-A.2">A.2</a>.  Adding an Array Element</span>

   An example target JSON document:

   { "foo": [ "bar", "baz" ] }

   A JSON Patch document:

   [
     { "op": "add", "path": "/foo/1", "value": "qux" }
   ]

   The resulting JSON document:

   { "foo": [ "bar", "qux", "baz" ] }

<span class="h3"><a class="selflink" id="appendix-A.3" href="#appendix-A.3">A.3</a>.  Removing an Object Member</span>

   An example target JSON document:

   {
     "baz": "qux",
     "foo": "bar"
   }






<span class="grey">Bryan & Nottingham           Standards Track                   [Page 12]</span>

<span id="page-13" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


   A JSON Patch document:

   [
     { "op": "remove", "path": "/baz" }
   ]

   The resulting JSON document:

   { "foo": "bar" }

<span class="h3"><a class="selflink" id="appendix-A.4" href="#appendix-A.4">A.4</a>.  Removing an Array Element</span>

   An example target JSON document:

   { "foo": [ "bar", "qux", "baz" ] }

   A JSON Patch document:

   [
     { "op": "remove", "path": "/foo/1" }
   ]

   The resulting JSON document:

   { "foo": [ "bar", "baz" ] }

<span class="h3"><a class="selflink" id="appendix-A.5" href="#appendix-A.5">A.5</a>.  Replacing a Value</span>

   An example target JSON document:

   {
     "baz": "qux",
     "foo": "bar"
   }

   A JSON Patch document:

   [
     { "op": "replace", "path": "/baz", "value": "boo" }
   ]

   The resulting JSON document:

   {
     "baz": "boo",
     "foo": "bar"
   }




<span class="grey">Bryan & Nottingham           Standards Track                   [Page 13]</span>

<span id="page-14" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


<span class="h3"><a class="selflink" id="appendix-A.6" href="#appendix-A.6">A.6</a>.  Moving a Value</span>

   An example target JSON document:

   {
     "foo": {
       "bar": "baz",
       "waldo": "fred"
     },
     "qux": {
       "corge": "grault"
     }
   }

   A JSON Patch document:

   [
     { "op": "move", "from": "/foo/waldo", "path": "/qux/thud" }
   ]

   The resulting JSON document:

   {
     "foo": {
       "bar": "baz"
     },
     "qux": {
       "corge": "grault",
       "thud": "fred"
     }
   }

<span class="h3"><a class="selflink" id="appendix-A.7" href="#appendix-A.7">A.7</a>.  Moving an Array Element</span>

   An example target JSON document:

   { "foo": [ "all", "grass", "cows", "eat" ] }

   A JSON Patch document:

   [
     { "op": "move", "from": "/foo/1", "path": "/foo/3" }
   ]

   The resulting JSON document:

   { "foo": [ "all", "cows", "eat", "grass" ] }




<span class="grey">Bryan & Nottingham           Standards Track                   [Page 14]</span>

<span id="page-15" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


<span class="h3"><a class="selflink" id="appendix-A.8" href="#appendix-A.8">A.8</a>.  Testing a Value: Success</span>

   An example target JSON document:

   {
     "baz": "qux",
     "foo": [ "a", 2, "c" ]
   }

   A JSON Patch document that will result in successful evaluation:

   [
     { "op": "test", "path": "/baz", "value": "qux" },
     { "op": "test", "path": "/foo/1", "value": 2 }
   ]

<span class="h3"><a class="selflink" id="appendix-A.9" href="#appendix-A.9">A.9</a>.  Testing a Value: Error</span>

   An example target JSON document:

   { "baz": "qux" }

   A JSON Patch document that will result in an error condition:

   [
     { "op": "test", "path": "/baz", "value": "bar" }
   ]

<span class="h3"><a class="selflink" id="appendix-A.10" href="#appendix-A.10">A.10</a>.  Adding a Nested Member Object</span>

   An example target JSON document:

   { "foo": "bar" }

   A JSON Patch document:

   [
     { "op": "add", "path": "/child", "value": { "grandchild": { } } }
   ]












<span class="grey">Bryan & Nottingham           Standards Track                   [Page 15]</span>

<span id="page-16" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


   The resulting JSON document:

   {
     "foo": "bar",
     "child": {
       "grandchild": {
       }
     }
   }

<span class="h3"><a class="selflink" id="appendix-A.11" href="#appendix-A.11">A.11</a>.  Ignoring Unrecognized Elements</span>

   An example target JSON document:

   { "foo": "bar" }

   A JSON Patch document:

   [
     { "op": "add", "path": "/baz", "value": "qux", "xyz": 123 }
   ]

   The resulting JSON document:

   {
     "foo": "bar",
     "baz": "qux"
   }

<span class="h3"><a class="selflink" id="appendix-A.12" href="#appendix-A.12">A.12</a>.  Adding to a Nonexistent Target</span>

   An example target JSON document:

   { "foo": "bar" }

   A JSON Patch document:

   [
     { "op": "add", "path": "/baz/bat", "value": "qux" }
   ]

   This JSON Patch document, applied to the target JSON document above,
   would result in an error (therefore, it would not be applied),
   because the "add" operation's target location that references neither
   the root of the document, nor a member of an existing object, nor a
   member of an existing array.





<span class="grey">Bryan & Nottingham           Standards Track                   [Page 16]</span>

<span id="page-17" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


<span class="h3"><a class="selflink" id="appendix-A.13" href="#appendix-A.13">A.13</a>.  Invalid JSON Patch Document</span>

   A JSON Patch document:

   [
     { "op": "add", "path": "/baz", "value": "qux", "op": "remove" }
   ]

   This JSON Patch document cannot be treated as an "add" operation,
   because it contains a later "op":"remove" element.  JSON requires
   that object member names be unique with a "SHOULD" requirement, and
   there is no standard error handling for duplicates.

<span class="h3"><a class="selflink" id="appendix-A.14" href="#appendix-A.14">A.14</a>.  ~ Escape Ordering</span>

   An example target JSON document:

   {
     "/": 9,
     "~1": 10
   }

   A JSON Patch document:

   [
     {"op": "test", "path": "/~01", "value": 10}
   ]

   The resulting JSON document:

   {
     "/": 9,
     "~1": 10
   }

<span class="h3"><a class="selflink" id="appendix-A.15" href="#appendix-A.15">A.15</a>.  Comparing Strings and Numbers</span>

   An example target JSON document:

   {
     "/": 9,
     "~1": 10
   }








<span class="grey">Bryan & Nottingham           Standards Track                   [Page 17]</span>

<span id="page-18" ></span>
<span class="grey"><a href="./rfc6902">RFC 6902</a>                       JSON Patch                     April 2013</span>


   A JSON Patch document:

   [
     {"op": "test", "path": "/~01", "value": "10"}
   ]

   This results in an error, because the test fails.  The document value
   is numeric, whereas the value being tested for is a string.

<span class="h3"><a class="selflink" id="appendix-A.16" href="#appendix-A.16">A.16</a>.  Adding an Array Value</span>

   An example target JSON document:

   { "foo": ["bar"] }

   A JSON Patch document:

   [
     { "op": "add", "path": "/foo/-", "value": ["abc", "def"] }
   ]

   The resulting JSON document:

   { "foo": ["bar", ["abc", "def"]] }

Authors' Addresses

   Paul C. Bryan (editor)
   Salesforce.com

   Phone: +1 604 783 1481
   EMail: [email protected]


   Mark Nottingham (editor)
   Akamai

   EMail: [email protected]













Bryan & Nottingham           Standards Track                   [Page 18]

Additional Resources