Skip to content

RFC 6902 (JSON Patch)

shagaba edited this page Sep 10, 2016 · 1 revision

##Operations Inspired by RFC 6902 (JSON Patch) :

Add

The "add" operation performs one of the following functions, depending upon what the target location references:

  • If the target location specifies an array index, a new value is inserted into the array at the specified index.
  • If the target location specifies an object member that does not already exist, a new member is added to the object.
  • 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.
{
	"op" : "add", 
	"path" : "/author/firstName", 
	"value" : "James" 
}

Remove

The "remove" operation removes the value at the target location. The target location MUST exist for the operation to be successful. If removing an element from an array, any elements above the specified index are shifted one position to the left.

{
	"op" : "remove",
	"path" : "/author/email" 
}

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. 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.

{ 
	"op" : "replace", 
	"path" : "/sections/3/paragraphs/2", 
	"value" : { 
		"title" :  "Paragraph Title", 
		"content" : "paragraph content" 
	} 
}

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, 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. 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. The "from" location MUST NOT be a proper prefix of the "path" location, a location cannot be moved into one of its children.

{
	"op" : "move", 
	"from" : "/sections/3/paragraphs/2", 
	"path" : "/sections/3/paragraphs/4" 
}

Copy

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. This operation is functionally identical to an "add" operation at the target location using the value specified in the "from" member.

{
	"op" : "copy", 
	"from" : "/sections/3/paragraphs/2", 
	"path" : "/sections/3/paragraphs/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 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:

  • strings: are considered equal if they contain the same number of Unicode characters and their code points are byte-by-byte equal.
  • numbers: are considered equal if their values are numerically equal.
  • 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.
  • 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).
  • literals (false, true, and null): are considered equal if they are the same.
{
	"op" : "test", 
	"path" : "/author/firstName", 
	"value" : "James" 
}
Clone this wiki locally