stollz
January 3, 2021, 10:19am
1
Hi there,
I went through this forum already, tried all the tips, but I am still not able to solve this:
a result of an http node is a single item ““colors””, I need to split this into multiple items, so I can iterate over blue, red…
[
{
"colors": [
{
"id": 1,
"name": "blue"
},
{
"id": 2,
"name": "red"
}
]
}
]
stollz
January 3, 2021, 10:40am
2
{
"nodes": [
{
"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": [
70,
1050
]
},
{
"parameters": {
"functionCode": "items[0].json = \n{\n \"color\": [\n {\n \"id\": 1,\n \"name\": \"blue\",\n },\n {\n \"id\": 2,\n \"name\": \"red\",\n }\n]\n};\nreturn items;"
},
"name": "Create Data",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
-110,
1050
]
}
],
"connections": {
"Create Data": {
"main": [
[
{
"node": "Create array",
"type": "main",
"index": 0
}
]
]
}
}
}
stollz
January 3, 2021, 10:42am
3
this is what I tried. But I got error in second node:
““Cannot convert undefined or null to object””
jan
January 3, 2021, 11:39am
4
Welcome to the community @stollz !
You can adapt what is documented here:
return items[0].json.color.map(item => {
return {
json: item
}
});
stollz
January 3, 2021, 1:31pm
5
Thanks @jan here is the solution:
( I needed to add the color ```
const data = items[0].json[“color”];
1 Like
jan
January 3, 2021, 2:13pm
6
Would still suggest to use the solution I did post as it is simpler, does everything in one step and does not produce an invalid state inbetween with the data being an array.
stollz
January 3, 2021, 2:41pm
7
you are right, I removed the step in the middle. Less nodes, same result. Thank you very much @jan