Hey Everyone!
I’m going crazy trying to return an array of Objects with the Function node as it either tells me that “All returned items have to contain a property named JSON” or that the return type must be an array of objects.
Below is the JSON file I currently get. I would like to return the same but without having the outer array.
But whener I try to return for example items[0] (Which in my mind it should return the first object of the outer array which in this case is the inner array) n8n gives me the above-mentioned error (…property named JSON).
[
[
{
"Name": ".......",
"Url": "........",
"Description": ".......",
"list": "........"
},
{
"Name": ".......",
"Url": "........",
"Description": ".......",
"list": "........"
}
]
]
Here is my code:
//JSON LENGHT
len_json = Object.keys(items[0].json).length;
console.log(len_arr)
final_array = [];
for (let i = 1; i <= len_json; i++) {
//Companies array length
var len_arr = items[0].json[i].Companies.length;
for(let j = 0; j < len_arr; j++){
name = items[0].json[i].Companies[j].split('(')[0].trim()
//console.log("name " + name);
url = items[0].json[i].Companies[j].substring(items[0].json[i].Companies[j].lastIndexOf("(") + 1, items[0].json[i].Companies[j].lastIndexOf(")")).trim();
//console.log("url " + url);
descr = items[0].json[i].Companies[j].split(')').pop().trim();
//console.log("Descr " + Descr);
items[0].json[i].Companies[j] = {Name : name, Url: url, Description : descr, list : items[0].json[i].type};
final_array.push(items[0].json[i].Companies[j]);
}
}
for (let x = 1; x <= len_json; x++){
array = items[0].json[x].Companies
items[0].json[x] = array
}
arrayToString = JSON.stringify(final_array);
stringToJsonObject = JSON.parse(arrayToString);
return [{json: stringToJsonObject}];
I know that the last line is not ideal but it is the only way I could make it work.
Any help is greatly appreciated Thanks!