How to use the Code node to create multiple items from a single item?

Hello guys!

I’m trying to use the Code node to tranform a single item to multiple as this described in documentation here - Understanding the data structure | n8n Docs

My input array is:

[
    {
        "name": "nnn",
        "email": {
            "personal": "[email protected]",
            "work": "[email protected]"
        }
    }
];

JS-code of the Code node (Run Once for All Items):

return $input.all().map(item => {
	return {
		json: item
	}
});

and outpiut, surprisingly, here:

[
    {
        "json": {
            "name": "nnn",
            "email": {
                "personal": "[email protected]",
                "work": "[email protected]"
            },
            "name2": "nnn2"
        },
        "pairedItem": {
            "item": 0
        }
    }
]

– so the output doesn’t look as “multiple items” in any way (for me) :frowning:

What am I doing wrong and what have to do to provide multiple items?
(if possible I would ask also for some explanations, how does it works in JS, and also - what is it ‘pairedItem’?)

It looks like your topic is missing some important information. Could you provide the following if applicable.

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

Welcome to the community @Sergii_Matsiupa !

Tip for sharing information

Pasting your n8n workflow


Ensure to copy your n8n workflow and paste it in the code block, that is in between the pairs of triple backticks, which also could be achieved by clicking </> (preformatted text) in the editor and pasting in your workflow.

```
<your workflow>
```

That implies to any JSON output you would like to share with us.

Make sure that you have removed any sensitive information from your workflow and include dummy or pinned data with it!


In order to split the item it has to have the data that could be split in the first place. Your item dors not haver such a data. What outcome would you like to see when multiplying the item?

For clarity, he’s an example of an item that could be split

{
    "data": [
        {
            "name": "AAA",
            "email": {
                "personal": "[email protected]",
                "work": "[email protected]"
            }
        },
        {
            "name": "AAA",
            "email": {
                "personal": "[email protected]",
                "work": "[email protected]"
            }
        }
    ]
}

As it is a single item, you do not have to use all(). Instead you can use first() as in

return $input.first().json.data.map(item => {
	return {
		json: item
	}
});
1 Like

Thanks a lot for help! :handshake:
Well, seems also your article here Understanding the data structure | n8n Docs heeds to be corrected because code snippet

return $input.all().map(item => {
	return {
		json: item
	}
});

came literally from that article

Hey @Sergii_Matsiupa , thank you for pointing out at the “broken” code. We shall fix it in the doc.

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