Insert, remove and replace array elements
03Array positions are zero-based and move as elements are inserted or removed. A supplied array is one value, not a spread instruction.
add /items/- with value["x","y"] yields a third element that is itself an array.
Source for this drawing: RFC 6902 · Read RFC section 4.1 ↗
New to this? Start hereA quick picture + helpful background
First, agree how to read the message.
An encoding tells a reader how to interpret bytes. A format tells it how values fit together. Correctly reading a value does not make that value trustworthy.
Helpful first, if these ideas are unfamiliar
These pictures are analogies to help you learn. The linked RFC explains the actual rules.
Terms in this drawing
Related RFCs and changes
“Updates” changes part of an earlier RFC. “Obsoletes” replaces it. This list shows selected related documents.
Resolve every index against the array at that moment.
- Array add inserts before the named index and shifts elements at that index or above right. The index MUST NOT exceed the current length; an index equal to the length, or the special final token -, appends. Array indices have no leading zeros except0 itself.
- remove requires an existing target and shifts later elements left. replace also requires an existing target and a value member; it changes that element without insertion. The - append token is not an existing element to remove or replace.
- An add whose value is an array inserts that entire array as one element. In the example, adding ["x","y"] at /items/- increases the outer length by one, not two. Object-member add and array insertion are different cases.
Each state continues the same fictional items array. Initial["A","B"] becomes["A","G","B"], then["G","B"], then["G","D"], then["G","D",["x","y"]]. The drawings show element slots, not byte widths.
Read the RFC section ↗