JSON nested arrays, Function and Split In Batches

Hello. I am a new user of n8n. First off, thanks for the great tool, I am enjoying working with it!

Onto the problem:
I am trying to setup a workflow where I retrieve a set of calendar events through a HTTP node and create events in my Google Calendar.
The HTTP API call returns JSON data which is itself an array. This is the output when I use cURL on my terminal:

[
  {}, // Event 1
  {}  // Event 2
]

Unfortunately in the workflow this shows up at the input of Split In Batches (after HTTP node) like this:

[ 
  [
    {}, // Event 1
    {}  // Event 2
  ]
]

I think Split in Batches is considering this as one item (my batch size is 1) so my Google Calendar event fires only once. I tried adding a Function node, but that still outputs as a nested list.

No matter what I do I am unable to transform the data to a format that Split in Batches can understand. Could you help please? Thanks.

Here is a workflow showing reproduction of the error:

  "name": "Nested-Array",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const item = \n[{\n  \"json\": [\n       {\"subject\": \"Lecture A\", \"startTime\": \"X\"},\n       {\"subject\": \"Lecture B\", \"startTime\": \"Y\"},\n       {\"subject\": \"Lecture C\", \"startTime\": \"Z\"}\n   ]\n}];\n\nreturn item;\n"
      },
      "name": "API Output",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        490,
        290
      ]
    },
    {
      "parameters": {
        "functionCode": "return item;"
      },
      "name": "return item",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        780,
        320
      ]
    },
    {
      "parameters": {
        "functionCode": "return item[0];"
      },
      "name": "return item[0]",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        780,
        140
      ]
    },
    {
      "parameters": {
        "functionCode": "const newItems = [];\nsubArray = items[0]\nfor (let i=0;i<subArray.length;i++) {\n  newItems.push({\n    json: {\n      startTime: subArray.startTime,\n      subject: subArray.subject      \n    }\n  });\n}\n\nreturn items;\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        780,
        690
      ]
    },
    {
      "parameters": {
        "functionCode": "newItems = [];\n\nfor (let i=0;i<item.length;i++) {\n  newItems.push({\n    json: {\n      startTime: item[i].startTime,\n      subject: item[i].subject      \n    }\n  });\n}\n\nreturn newItems;\n"
      },
      "name": "Populate New Array",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        780,
        510
      ]
    }
  ],
  "connections": {
    "API Output": {
      "main": [
        [
          {
            "node": "return item[0]",
            "type": "main",
            "index": 0
          },
          {
            "node": "return item",
            "type": "main",
            "index": 0
          },
          {
            "node": "Populate New Array",
            "type": "main",
            "index": 0
          },
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "2"
}

Just to clarify, I am trying to go from:

[ 
  [
    {}, // Event 1
    {}  // Event 2
  ]
]

to

  [
    {}, // Event 1
    {}  // Event 2
  ]

Because the latter is what Split in batches understands according to docs.
Thanks folks.

Welcome to the community @k-metric

Check the example below.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://mockup-pj5l0yxsjrbr.runkit.sh/",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        520,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const results = []\n\nconst events = items[0].json\n\nfor (event of events) {\n  results.push({ json: event })\n}\n\nreturn results;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        760,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Thank you @RicardoE105! Worked great.
Appreciate your help.

Glad that it worked. Let’s know if you have other questions.