ModelWorks logoModelWorks
JSON TransformArray

insertAt

Insert one or more elements at a specific index

insertAt

Inserts items into the array starting at index, shifting existing elements to the right. If items is itself an array, the elements are inserted individually (spread); otherwise items is inserted as a single element.

Options

OptionTypeRequiredDefaultDescription
indexintegerYesZero-based insertion index. Clamped to array length.
itemsanyYesItem or array of items to insert. Selectors resolved.

Example

Input

JSON
["a", "d"]

Operation

JSON
{ "type": "insertAt", "options": { "index": 1, "items": ["b", "c"] } }

Output

JSON
["a", "b", "c", "d"]

In a Circuit Step

JSON
{
  "name": "inject_context_block",
  "description": "Insert two context messages just after the system prompt",
  "function": "circuit.core.transform.json",
  "input": {
    "source": "${{ messages }}",
    "operations": [
      {
        "type": "insertAt",
        "options": {
          "index": 1,
          "items": [
            { "role": "user", "content": "${{ input.context_a }}" },
            { "role": "user", "content": "${{ input.context_b }}" }
          ]
        }
      }
    ]
  },
  "outputs": [
    {
      "name": "messages_with_context",
      "description": "Conversation with two context messages inserted at index 1",
      "value": "${{ inject_context_block.output.transformed_result }}"
    }
  ]
}

Notes & gotchas

  • Errors with insertAt: expected array if the input is not an array.
  • index is clamped to the current length, so index: 999 appends rather than failing.
  • When items is an array, elements are inserted in order: insertAt(1, ["b","c"]) on ["a","d"] yields ["a","b","c","d"], not ["a","c","b","d"].
  • items is fully selector-evaluated, including nested entries.
On this page

On this page