Visual RFC.
ALL RFCs / RFC 6902
Read the RFC ↗
RFC 6902 / JSON PATCH

Insert, remove and replace array elements

03

Array positions are zero-based and move as elements are inserted or removed. A supplied array is one value, not a spread instruction.

RFC 6902 · § 4.1EXAMPLE
5 / 5
Insert, remove and replace array elementsOne continuing array starts A,B. Insertion G at index1 moves B to2; removing index0 moves G and B left; replace index1changes B toD without growth; adding array x,y at dash appends one nested element. Each slot has an explicit current zero-based index; arrows trace the changed position and captions distinguish insertion, deletion and replacement.Array positions change when elements move{"op":"add","path":"/items/-","value":["x","y"]}After this operation"G"index 0"D"index 1["x","y"]index 2Outer array length: 3One nested valueAppend one element · do not spread x and yadd: 0..length or - · remove/replace: existing index
05
Append a nested array as one element.

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 ↗

Protocol participantThreat or exceptionProtocol controlSimplified drawings · precise section references
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.

Related RFCs and changes

“Updates” changes part of an earlier RFC. “Obsoletes” replaces it. This list shows selected related documents.

KEY POINT

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.
DETAILS

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 ↗

All chapters in RFC 6902

Drawing view

Scroll the drawing to see every part. Playback controls stay above it.

RFC 9700 / CHAPTERS

Chapters

For full details, tables, algorithms, and exceptions, read the original RFC.

RFC status and corrections ↗