Data strucre in the code

Hey ,

I saw this in the documentation

Notice the format in which we return the results of the calculation:

let items = $input.all();
let totalBooked = items.length;
let bookedSum = 0;

for (let i=0; i < items.length; i++) {
  bookedSum = bookedSum + items[i].json.orderPrice;
}

return [{ json: {totalBooked, bookedSum} }];

Data structure error

If you don’t use the correct data structure, you will get an error message: Error: Always an Array of items has to be returned!

But when I make this it works why ?

return {totalBooked, bookedSum} ;

My complete code :

let items = $input.all();let totalBooked = items.length;let bookedSum = 0;

for (let i=0; i < items.length; i++) {bookedSum = bookedSum + items[i].json.orderPrice;}

return {totalBooked, bookedSum} ;

Hi @toure5013 welcome to the :n8n: community

I believe this is handled internally by design, See this mention in the text course:

It’s good practice to pass the data in the right structure used by n8n. But don’t worry if you forget to add the json key to an item, n8n (version 0.166.0 and above) adds it automatically.

Also this hint:

1 Like

Hi @mohamed3nan ,

Well noted. Thank you !