How to return a Json Array

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 :slight_smile: Thanks!

Hi @Spacewalker, welcome to the community! I hope you’re having a good day thus far?

So at the moment, you are getting a single item which is essentially an array? If so, you could use something like return items[0].json.map(e => {return {json: e}}); in your function node to return an array of items with a json key in each item as required by n8n’s data structure.

As a complete workflow this would look like so (the first node sets your example data, the second transforms it):

So this:

…becomes this:

I hope this helps and makes sense! Please do let me know if you have any further queries here or in case you’re still facing trouble. Maybe you can also share your workflow (minus anything confidential, of course) if so?

2 Likes

Hi @MutedJam,

Thank you very much for the time, this indeed solves my problems.

Have a great day!

2 Likes

Glad to hear that, thanks so much for your feedback!

Can I ask you for a small favour here: Would you be able to mark the response as Solution? This helps the community keeping track of open questions still waiting for an answer and those that have already been answered.

Many thanks!

3 Likes