Create a JSON object as input for Loop over Items node

Describe the problem/error/question I want to create a loop that runs N times. So, I want to create a JSON object with 5 items to be send to the Loop over Items node. Apparently, I am struggling with the right syntax of the JSON object. What should it be. For example to run the loop 5 times. I want to place this object into the set node which is connected to the Loop over Items node

What is the error message (if any)? No error message

Please share your workflow


(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and {
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        1648,
        -192
      ],
      "id": "f489e7a1-9673-419f-ad66-a6f1b9b66136",
      "name": "When clicking ‘Execute workflow’"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "d2b1532d-e8b3-4788-82b2-d60e6f08a0c9",
              "name": "",
              "value": "\t{ \t\tcolor: \"red\", \t\tvalue: \"#f00\" \t}, \t{ \t\tcolor: \"green\", \t\tvalue: \"#0f0\" \t}, \t{ \t\tcolor: \"blue\", \t\tvalue: \"#00f\" \t}, \t{ \t\tcolor: \"cyan\", \t\tvalue: \"#0ff\" \t}, \t{ \t\tcolor: \"magenta\", \t\tvalue: \"#f0f\" \t}, \t{ \t\tcolor: \"yellow\", \t\tvalue: \"#ff0\" \t}, \t{ \t\tcolor: \"black\", \t\tvalue: \"#000\" \t} ",
              "type": "object"
            }
          ]
        },
        "options": {
          "ignoreConversionErrors": true,
          "dotNotation": false
        }
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1872,
        -192
      ],
      "id": "f71838ad-0bb1-4f21-8477-997db8fd5335",
      "name": "Items list"
    },
    {
      "parameters": {
        "batchSize": 4,
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        2064,
        -192
      ],
      "id": "26a8214d-7cde-4fad-b1f7-f814557b99ad",
      "name": "Loop Over Items1"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.noOp",
      "name": "Replace Me1",
      "typeVersion": 1,
      "position": [
        2384,
        -160
      ],
      "id": "6667bfdd-0971-4c7a-9439-9d27a7cf3438"
    }
  ],
  "connections": {
    "When clicking ‘Execute workflow’": {
      "main": [
        [
          {
            "node": "Items list",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Items list": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items1": {
      "main": [
        [],
        [
          {
            "node": "Replace Me1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Replace Me1": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "a256caaf927d8b6fecd66012f15459060ec2fca55c23cb3757c0092517d4e8b9"
  }
}CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:
1 Like

Hey @Jitse_Schaafsma

The Loop Over Items node doesn’t loop over an array inside one item it loops over the items themselves. So to run the loop 5 times, you simply need to give it 5 items as input, for example:

[
  { "json": { "index": 1 } },
  { "json": { "index": 2 } },
  { "json": { "index": 3 } },
  { "json": { "index": 4 } },
  { "json": { "index": 5 } }
]

This matches n8n’s data structure: an array of objects, each with a json key.

Easiest way: Code node before Loop Over Items

Instead of trying to “fake” JSON in a Set node, add a Code node before Loop Over Items and use:

// Run Once for All Items
const items = [];

for (let i = 1; i <= 5; i++) {
  items.push({ json: { index: i } });
}

return items;

This produces 5 items, so Loop Over Items (batch size 1) will run 5 iterations, one per item.

Inside your loop, you can then use {{$json.index}} if you need the current iteration number.

Your current Set node value is a single item with a string that looks like an array; the Loop node will therefore only see 1 item and run once. Converting it to multiple items as above will make the loop behave as you expect. Let me know if this helps! Most of the times using Code Node solves a lot of problems for data manipulation operations, give it a shot!

Cheers!

1 Like

Thank you, that does the trick.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.