Doubts with Array

Hey guys!

I’m trying to return items with this structure:

So that they look like this (outside the array):
2

So you want to map the first picture to the second picture? Is it the first output coming from an HTTP node?

Yes, @RicardoE105, that’s the idea. I generated this data artificially for testing as I have a bigger workflow with xml files.

If every inner array has a single item, then:

return items.map(i => ({ json: i.json[0] }));
1 Like

Thank you very much, @ivov ! Now I understand the structure of the data well.

One more question arose. And in case the internal array has one more object. How do I return in this structure?

I’ve been stuck in this second situation for days. Any help is valid.

Dude, sorry for the late response. In that case, the function node should look like this:

const response = []

for (item of items) {
  for (data of item.json) {
    response.push({
      json: data
    })
  }
}
return response
Example workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -1910,
        40
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: [\n      {\n        name: 'ricardp'\n      },\n            {\n        name: 'ricardp'\n      }\n    ]\n  },\n    {\n    json: [\n      {\n        name: 'ricardp'\n      },\n            {\n        name: 'ricardp'\n      }\n    ]\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -1650,
        30
      ]
    },
    {
      "parameters": {
        "functionCode": "const response = []\n\nfor (item of items) {\n  for (data of item.json) {\n    response.push({\n      json: data\n    })\n  }\n}\nreturn response"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -1380,
        30
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
2 Likes

Wow, @RicardoE105! Your solution worked perfectly well! Thank you so much for everyone’s help!