Need help with batch

i have a case that i get an array of jsons in test, i run a function to parse them:

    {
      "nodes": [
        {
          "parameters": {
            "functionCode": "return JSON.parse(item.stdout);"
          },
          "name": "Convert raw to JSON",
          "type": "n8n-nodes-base.functionItem",
          "typeVersion": 1,
          "position": [
            810,
            300
          ]
        }
      ],
      "connections": {}
    }

and i get from it an array of jsons nested in the item[0].
it looks like this:
image

i would like to batch on the array, but the batch gets the whole array as iteration 1 and finishes.

and i can’t make the ‘items’ array be the array itself (something I’m missing in how n8n builds its items)
looked at https://docs.n8n.io/#/data-structure
is there a fast way to get a json array as text and parse it and iterate over it with batch?

Sorry, not sure if I understand you correctly. But it sounds like you have an array in item 0 and you want to have each element of the array in an own item instead?

If that is the case you can do something like that:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      someArray: [\n        {\n          a: 1\n        },\n        {\n          a: 2\n        },\n        {\n          a: 3\n        }\n      ]\n    }\n  }\n]"
      },
      "name": "Mock-Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.someArray.map((item) => {\n  return {\n    json: item\n  }\n});\n"
      },
      "name": "Function-Split",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Mock-Data": {
      "main": [
        [
          {
            "node": "Function-Split",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

nice, it worked, my problem was the JSON i returned, and the fact that it was not in
{
json:
}
format.

thanks!

Yes, it is very important the data has always a json-property and that it is always an object as described here:
https://docs.n8n.io/#/data-structure

Everything in n8n is built around that.

Glad to hear that it works now. Have fun!