How do I extract data from a nested array?

Hi everyone. I understand that for an array of items I can simply do the following to extract the item I want

return [items[2]]

However if the item contains keys, and I want to extract for example the 1st key, how would I go about doing this? Is there a similarly simple solution?

Thanks :slight_smile:

Hey @aziadlourad,

If the array contains objects, and you want to refer to a specific value of the first object in the array, you can use something like items[0]['KEY']

If you can share sample data, it would help me better understand :slight_smile:

Hi @harshil1712 . Here is some sample code. I want to access the data within “events”.

[
  {
    "headers": {},
    "params": {},
    "query": {},
    "body": {
      "webhook_id": "testing",
      "events": [
        {
          "event_type": "testing0",
          "collection_id": "testing1",
          "entry_id": "testing2",
          "attribute_id": "testing3"
        }
      ]
    }
  }
]

Thanks! I’m sharing an example workflow that might help

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {json:{\n    \"headers\": {},\n    \"params\": {},\n    \"query\": {},\n    \"body\": {\n      \"webhook_id\": \"testing\",\n      \"events\": [\n        {\n          \"event_type\": \"testing0\",\n          \"collection_id\": \"testing1\",\n          \"entry_id\": \"testing2\",\n          \"attribute_id\": \"testing3\"\n        }\n      ]\n    }\n  }}\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        250
      ]
    },
    {
      "parameters": {
        "functionCode": "const event_type = $json[\"body\"][\"events\"][0][\"event_type\"];\nconst first_item = $json[\"body\"][\"events\"][0];\nconst first_key_value = first_item[Object.keys(first_item)[0]]\n\nreturn [{json:{event_type, first_key_value}}]"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        800,
        250
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

The first Function node simply mocks the data that you provided. The next Function node contains code where I am referencing the values.
If you always want to return the value for event_type, the snippet for the first variable will be helpful.
If you want to return always the first item (it can be anything, not necessary to be event_type), the code snippets for the variable first_item and first_key_value will be useful.

I hope this helps :slight_smile:

Hi Harshil, the workflow above dosent seem to copy properly. It only shows one function nodde

Hey @aziadlourad,

Here’s the function node that is referencing the values

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const event_type = $json[\"body\"][\"events\"][0][\"event_type\"];\nconst first_item = $json[\"body\"][\"events\"][0];\nconst first_key_value = first_item[Object.keys(first_item)[0]]\n\nreturn [{json:{event_type, first_key_value}}]"
      },
      "name": "Ref value",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        800,
        250
      ]
    }
  ],
  "connections": {}
}

Hi Harshil,

The issue is that when I copy your code it makes one node with a circular loop around it for some reason.

Not sure why that is happening. But you can always disconnect and break the loop.

There is only one node being produced though. How many is there supposed to be?

Hey @aziadlourad,

The last workflow that I shared contains only one node. That’s the node that you need to refer.