ModelWorks logoModelWorks
JSON TransformArray

replaceAt

Replace the element at a specific index

replaceAt

Overwrites the element at index with value. If the index is out of range the array is returned unchanged.

Options

OptionTypeRequiredDefaultDescription
indexintegerYesZero-based index of the element to replace.
valueanyYesReplacement value (any JSON type).

Example

Input

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

Operation

JSON
{ "type": "replaceAt", "options": { "index": 1, "value": "BB" } }

Output

JSON
["a", "BB", "c"]

In a Circuit Step

JSON
{
  "name": "swap_second_message",
  "description": "Replace the second message in the conversation",
  "function": "circuit.core.transform.json",
  "input": {
    "source": "${{ messages }}",
    "operations": [
      {
        "type": "replaceAt",
        "options": {
          "index": 1,
          "value": { "role": "user", "content": "${{ input.replacement_text }}" }
        }
      }
    ]
  },
  "outputs": [
    {
      "name": "patched_messages",
      "description": "Conversation with element at index 1 replaced",
      "value": "${{ swap_second_message.output.transformed_result }}"
    }
  ]
}

Notes & gotchas

  • Errors with replaceAt: expected array if the input is not an array.
  • An out-of-range index is silently ignored — the array passes through unchanged. There is no error or growth.
  • value is not selector-evaluated by replaceAt itself. To inject a selector value, place the selector string into value upstream (outer selector resolution) or use map for conditional replacement.
  • To replace by condition rather than index, see replaceMatching.
On this page

On this page