One json item instead of multiple items

I’m trying to get this json response to be split in single items with the SplitinBatches node. But all items are somehow one item instead of 4 single items. How would I get this working?

{
  "nodes": [
{
  "parameters": {
    "functionCode": "return [\n{\n\t\"json\": [{\n\t\t\"24\": {\n\t\t\t\"quantity\": 1,\n\t\t\t\"sku\": \"SKU1\",\n\t\t\t\"lastModified\": \"2020-10-30T00:28:14+00:00\"\n\t\t},\n\t\t\"25\": {\n\t\t\t\"quantity\": 10,\n\t\t\t\"sku\": \"SKU2\",\n\t\t\t\"lastModified\": \"2020-10-30T00:28:17+00:00\"\n\t\t},\n \t\"28\": {\n\t\t\t\"quantity\": 8,\n\t\t\t\"sku\": \"SKU24\",\n\t\t\t\"lastModified\": \"2020-10-30T00:28:17+00:00\"\n\t\t},\n \t\"29\": {\n\t\t\t\"quantity\": 4,\n\t\t\t\"sku\": \"SKU13\",\n\t\t\t\"lastModified\": \"2020-10-31T00:24:17+00:00\"\n\t\t}\n\t}]\n}\n];"
  },
  "name": "Function1",
  "type": "n8n-nodes-base.function",
  "typeVersion": 1,
  "position": [
    530,
    300
  ]
},
{
  "parameters": {
    "batchSize": 1,
    "options": {}
  },
  "name": "SplitInBatches1",
  "type": "n8n-nodes-base.splitInBatches",
  "typeVersion": 1,
  "position": [
    760,
    300
  ]
}
  ],
  "connections": {
"Function1": {
  "main": [
    [
      {
        "node": "SplitInBatches1",
        "type": "main",
        "index": 0
      }
    ]
  ]
}
  }
}

Hey @paul123!

Your data has a single object within the array. The Spilt In Batches node considers this as one single batch and returns it. I have modified your workflow and made some changes to it. The Create array Function node, converts the object into an array. The Create JSON-items Function node, creates individual JSON items for the objects within the array. The Split In Batches node will now split the data into batches.

{
  "nodes": [
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "SplitInBatches1",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        990,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.map(item => { \n  return {\n    json: item,\n  }\n})"
      },
      "name": "Create JSON-items",
      "type": "n8n-nodes-base.function",
      "position": [
        800,
        300
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "return [\n{\n\t\"json\": [{\n\t\t\"24\": {\n\t\t\t\"quantity\": 1,\n\t\t\t\"sku\": \"SKU1\",\n\t\t\t\"lastModified\": \"2020-10-30T00:28:14+00:00\"\n\t\t},\n\t\t\"25\": {\n\t\t\t\"quantity\": 10,\n\t\t\t\"sku\": \"SKU2\",\n\t\t\t\"lastModified\": \"2020-10-30T00:28:17+00:00\"\n\t\t},\n \t\"28\": {\n\t\t\t\"quantity\": 8,\n\t\t\t\"sku\": \"SKU24\",\n\t\t\t\"lastModified\": \"2020-10-30T00:28:17+00:00\"\n\t\t},\n \t\"29\": {\n\t\t\t\"quantity\": 4,\n\t\t\t\"sku\": \"SKU13\",\n\t\t\t\"lastModified\": \"2020-10-31T00:24:17+00:00\"\n\t\t}\n\t}]\n}\n];"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const data = items[0].json;\nconst data_arr = Object.keys(data[0]).map(key => {\n    return data[0][key]\n})\n\nreturn [{json:data_arr}]"
      },
      "name": "Create array",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        640,
        300
      ]
    }
  ],
  "connections": {
    "Create JSON-items": {
      "main": [
        [
          {
            "node": "SplitInBatches1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mock Data": {
      "main": [
        [
          {
            "node": "Create array",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create array": {
      "main": [
        [
          {
            "node": "Create JSON-items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hope this helps!

1 Like

Many thanks @harshil1712 that works perfect! :facepunch:

1 Like

I am glad it worked! Have fun! :slightly_smiling_face: