stollz
3. Januar 2021 um 10:19
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
3. Januar 2021 um 10:40
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
3. Januar 2021 um 10:42
3
this is what I tried. But I got error in second node:
““Cannot convert undefined or null to object””
jan
3. Januar 2021 um 11:39
4
Welcome to the community @stollz !
You can adapt what is documented here:
return items[0].json.color.map(item => {
return {
json: item
}
});
stollz
3. Januar 2021 um 13:31
5
Thanks @jan here is the solution:
( I needed to add the color ```
const data = items[0].json[“color”];
jan
3. Januar 2021 um 14:13
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
3. Januar 2021 um 14:41
7
you are right, I removed the step in the middle. Less nodes, same result. Thank you very much @jan