I can't find samples on the way to use JSON Patch to update a collection. In fact, I want to use a method PATCH
on a collection REST resource in order to update the associated collection without sending again the whole collection. I wonder if JSON Patch could match to describe the operations to do: mainly add elements or remove elements. Elements are complex, meaning that they aren't primitive elements.
Below there are some more details. Let's take the sample of a resource contacts
:
GET /contacts
[
{
"id": "1",
"lastName": "Last name 1",
"firstName": "First name 1"
},
{
"id": "2",
"lastName": "Last name 2",
"firstName": "First name 2"
},
{
"id": "3",
"lastName": "Last name 3",
"firstName": "First name 3"
},
(...)
]
Here is the PATCH
request I would like to use but I'm not sure that is JSON Patch compliant:
PATCH /contacts
[
{
"op": "add", "value": {
"firstName": "my first name",
"lastName": "my last name"
}
},
{
"op": "remove", "path": "id=='1'"
}
]
My main issue is how to identify the element to delete based on its field id
. Is there dedicated expression for this? I thought about something like: id=='1'
.
Last question: is the response content targeted by JSON Patch?
Thanks very much by avance for your help! Thierry
You should be able to simply use the path for the resource to be deleted.
PATCH /contacts
[
{
"op": "add",
"path": "/-",
"value": {
"firstName": "my first name",
"lastName": "my last name"
}
},
{
"op": "remove",
"path": "/0"
}
]
Looking around, there seems to be some confusion about this, but the standard says "The "remove" operation removes the value at the target location" with example:
{ "op": "remove", "path": "/a/b/c" }