Help accessing array in JSON stream

Hi there!

I am new to javascript and n8n, and I am having some troubles accessing an array inside a JSON stream.

The JSON stream (coming from a Openweather Map node) looks like this:

 [ {
        "cod": "200",
        "message": 0,
        "cnt": 40,
        "list": [ {
            "whatever": "whatever",
             },
             {
            "whatever": "whatever",
             },
             {
            "whatever": "whatever",
             } ]
 } ]

And the array I am interested in is the one inside “list”.

If I write a (n8n node) function like this:

const stream = $items("OpenWeatherMap");

return stream[0].list;

I get an error:

ERROR: No data got returned. Always return an Array of items!

Error: No data got returned. Always return an Array of items!
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:70:19)
    at async /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:369:47

There is probably a silly mistake somewhere. Where do I fall short?
Thanks!!

Hey @wolf3
Welcome to the community!

You can try the below code snippet. It creates a new empty array, stores the list data into the stream variable, and then pushes it to the new array. We are then returning the newArr.

var newArr = []
var stream = items[0].json.list
newArr.push({json:stream})
return newArr;

You can read more about the function node on our official documentation.

Let me know if you need any further help.

If you want to have them as separate items you can also check out this topic:

Fantastic, thanks!!

So, just to get this straight: both solutions bring up “json” in their declarations:

var stream = items[0].json.list

and

return items[0].json.data.map( ... )

Is that “json” a method or it’s just an index name (which n8n requires to function)?

It is a property of the items that get passed around in n8n. Every item has to have it. You can find documentation about the data-structure n8n uses here:
https://docs.n8n.io/reference/data/data-structure.html#data-structure

1 Like

@jan Thank you so much!!