Why Does the Second Operation in a JSON Patch Change the Wrong List Item?
An integration prepares two removals from a list using the indexes shown in a preview. The first removal succeeds. The second removes a different item because the list has shifted. Every path is syntactically valid, yet the final result does not match the approved intent.
A JSON Patch is a sequence of operations, not a set of independent edits against the original document. Review it as a changing document from one step to the next.
Follow the specified operation order
RFC 6902 states that operations are applied sequentially, with each resulting document becoming the input to the next operation. Removing an array element shifts later elements one position to the left; adding one can shift elements to the right.
A path such as /items/2 therefore describes a position in the document at that moment. It does not permanently identify the business item that occupied position two when the patch was first planned.
The distinction becomes important whenever a patch contains multiple array edits.
Walk through a small list
Illustrative example: two intended removals
Start with the list Cedar, Birch, Maple, Oak at indexes zero through three. The intent is to remove Birch and Maple, leaving Cedar and Oak.
Removing index one first leaves Cedar, Maple, Oak. Removing index two next removes Oak, producing the wrong result. The original index of Maple is no longer its current index.
Write the intermediate list beside every operation during review. This makes the identity error visible before any real data changes.
Compare two correct sequences for this fixture
For this specific pair of removals, removing the higher original index first—two, then one—produces the intended result. Removing index one twice also works because Maple shifts into index one after Birch is removed.
These are fixture-specific sequences, not a general instruction to reorder arbitrary patches. Adds, moves, tests and dependencies between operations require their own reasoning.
Preserve business identity in the review
Show the stable identifiers of intended items alongside their current positions. A reviewer should approve “remove these two items,” not merely a pair of integers whose meaning can change.
If another actor can modify the list between reading and writing, index reasoning alone is insufficient. Use the API's documented version or conditional-update mechanism where available, and re-evaluate the patch against the correct version.
The RFC also defines a test operation for checking an expected value. Whether and how to combine it with other consistency controls depends on the supported API contract.
Test failure behavior explicitly
Include an out-of-range position, a failed expected-value test and a patch built from an outdated document. Verify the receiver's documented error behavior and the final resource state.
Do not retry the same patch blindly after an uncertain response. If the first application already changed the list, repeating index-based removals may affect different items.
Read the current resource or use the operation's documented reconciliation mechanism before deciding the next action. Preserve the intended item identities in the progress record.
Validate the complete final list
A successful status is only one check. Compare the final item identifiers, order and relevant values with the expected result. Confirm that neighboring items remain intact and no intended removal was missed.
Keep a tiny before-and-after fixture with the patch builder. It should include adjacent removals, separated removals and any add or move combinations the application actually supports.
For pointer syntax within each path, see escaping JSON Pointer field names. A FutureOS integration brief can include the original list, intended final list and the intermediate state after each proposed operation.
The most useful review question is not whether every index existed at the start. It is whether each operation still targets the intended item when its turn arrives.
Editorial note
Editorial note: This article was prepared with AI assistance. Primary sources and the linked FutureOS article inventory were checked on October 26, 2026. Examples are illustrative and are not customer results. The current FutureOS introduction describes the project as being in prelaunch; this guide does not assert that a particular product feature, integration or service is available. The procedures are practical proposals to adapt and test in your own setting. No independent expert review is claimed. Start with a specific question, your relevant constraints and a small example of the result you need.




Comments