Skip to main content

Appendix A. Examples

A.1. Adding an Object Member​

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"
}

A.2. Adding an Array Element​

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" ] }

A.3. Removing an Object Member​

An example target JSON document:

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

A JSON Patch document:

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

The resulting JSON document:

{ "foo": "bar" }

A.4. Removing an Array Element​

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" ] }

A.5. Replacing a Value​

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"
}

A.6. Moving a Value​

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"
}
}

A.7. Moving an Array Element​

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" ] }

A.8. Testing a Value: Success​

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 }
]

A.9. Testing a Value: Error​

An example target JSON document:

{ "baz": "qux" }

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

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

A.10. Adding a Nested Member Object​

An example target JSON document:

{ "foo": "bar" }

A JSON Patch document:

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

The resulting JSON document:

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

A.11. Ignoring Unrecognized Elements​

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"
}

A.12. Adding to a Nonexistent Target​

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.

A.13. Invalid JSON Patch Document​

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.

A.14. ~ Escape Ordering​

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
}

A.15. Comparing Strings and Numbers​

An example target JSON document:

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

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.

A.16. Adding an Array Value​

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"]] }