3

In JSON patch, how should one use the index for subsequent operation on the same array. For example, consider

 var source = { colors: ['Red', 'Green', 'Blue'] };
 var target = { colors: [] }; 

patch doc (operations)

[{"op":"remove","path":"/colors/0"},
{"op":"remove","path":"/colors/1"},
{"op":"remove","path":"/colors/2"}]

If I consider the index of the source, the above indexes are correct. When I apply it sequentially though, the index are incorrect. That is if I remove, 0 th and 1st index, there is no element at index 2.

I could think of couple of ways to handle this. Either group all delete operation on an array, then keep a temporary structure to hold/manipulate change in index during deletion. Or, keep the index relative to a mutating value

[{"op":"remove","path":"/colors/0"},
{"op":"remove","path":"/colors/0"},
{"op":"remove","path":"/colors/0"}]

it makes sense if the operation is considered mutation of a resource in sequence.

Is there any standard on this. I cannot see anything about it in the spec. A.4. Removing an Array Element

Community
  • 1
  • 1
bsr
  • 57,282
  • 86
  • 216
  • 316

1 Answers1

4

Is there any standard on this

The section about the evaluation of the operations seems to be quite clear:

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.
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375