Get first child of json elements

Hi everyone,
I’m trying to get the first child of a json but I can’t get it, I’m using the following in a MySQL node to get the first email:

{{$node[“FunctionItem”].json[0][“email”]}}

But it doesn’t work, does anyone know why? The json I get it from a query to the database.

Hi @victormiranda,

That is an array so you would need to use items[0] if you want just the first one.

Quick Edit… Below is an example workflow that will show you what to do, The trick is in your function have return [items[0]];

  "name": "My workflow",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "id_customer",
              "value": 1234
            }
          ],
          "string": [
            {
              "name": "id_order_po",
              "value": "abc123"
            },
            {
              "name": "firstname",
              "value": "Jon"
            },
            {
              "name": "email",
              "value": "[email protected]"
            }
          ]
        },
        "options": {
          "dotNotation": false
        }
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        500,
        310
      ]
    },
    {
      "parameters": {
        "values": {
          "number": [
            {
              "name": "id_customer",
              "value": 4321
            }
          ],
          "string": [
            {
              "name": "id_order_po",
              "value": "133aaa"
            },
            {
              "name": "firstname",
              "value": "Bob"
            },
            {
              "name": "email",
              "value": "[email protected]"
            }
          ]
        },
        "options": {
          "dotNotation": false
        }
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        500,
        460
      ]
    },
    {
      "parameters": {},
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        700,
        380
      ]
    },
    {
      "parameters": {
        "functionCode": "return [items[0]];"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        900,
        380
      ]
    }
  ],
  "connections": {
    "Set1": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          },
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": 8
}```
2 Likes

@jon LOL!

Oh you are using a function item and not function, I missed that :joy: so the function item works in a different way and runs once for each item instead of running once for everything so with the function item it will always be dealing with each record as an item.

So if you wanted to access just the email address in your function item you would use item.email and that will run once for each of the emails. With the other function you would have to loop over the items or call one directly so it would be items[0].json.email.

If you just want the first item in your function maybe swapping to the function node would be better but it depends on what you want to do with the data in the future.

1 Like

I can’t obtain the array by child to child, and is all that i need.
I understand all that you comment, but i can’t solve this error.

For the javascript code you would just need item.email if it is being passed in from an input, $node[‘mysql’].json[0][“email”] probably doesn’t exist in the way that you would expect it to.

For your javascript just put…

console.log(item.email);
return item;

That will then give you the below so you can see how it looks in the browser dev console. From here you can write your function to do whatever you want.

Unless your function item is not directly after your MySQL query which is when it can get a bit more complicated.

image

Is the first time that it give me an array of this form.

image

That looks fine, So you can just use item.email if you want to use the email for something or any of the other options.

Rather than base it on the Nodes use Current Node which will give you the input parameters as well which can be handy further down your project.

Hey @victormiranda!

The Variable Selector always shows the first item. However, the next node will process all the incoming data. I hope this solves any confusion you had :slight_smile:

Let us know if you still need help

In the end I opted for another way, I did more queries, generating them with the LIMIT and OFFSET parameters that obtained values ​​from a counter. Thanks for your help friends.

1 Like