What is the JSON Patch format for removing an item from an array?

I have the following JSON document from which I want to remove the "roleId2" element from the value of the "role" field array:

{
  "id" : 12345,
  "firstName": "SomeFirstName",
  "lastName": "SomeLastName",
  "roles":["roleId1", "roleId2", "roleId3"]
}

      

How can I write a JSON Patch document to remove this item? Does the expression matter?

{"op": "remove", "path":"/roles", "value": "roleId2"}

      

Or, should it look like this (because the "role" value in the document is an array)?

{"op": "remove", "path":"/roles", "value": ["roleId2"]}

      

From reading RFC 6902 it is not clear to me if any one is correct. The RFC mentions the following behavior, but I'm not sure if that matters here.

When removing an element from an array, any elements above the specified index are shifted one position to the left.

+3


source to share


1 answer


Correct patch to remove item at index 1 from array:

{"op": "remove", "path": "/roles/1"}

      



See a working example at JSFiddle (using Fast-JSON-Patch )

+4


source







All Articles