Selecting a single item using functionItem

I want to select the item containing a particular string, e.g. I want to select the UserName for “john”, from the json.
json:

[
  {
    "UserName": "[email protected]",
    "Password": "Xyz-123"
  },
  {
    "UserName": "[email protected]", 
    "Password": "Xyz-123"
  },
  {
    "UserName": "[email protected]",
    "Password": "Xyz-123"
  }
]

The code that i have written, when executed returns no text data found, while I am returning an item.
Code:

for (var i = 0; i < item.length; i++) {
    if (item[i].UserName.toLowerCase().includes("john")) {
        return item[i];
    }
}

It looks like you are using a Function Item-Node when you should be using a Function-Node instead. You can find the documentation about it here which describes the difference and how both work:
https://docs.n8n.io/#/nodes?id=function-and-function-item-node

Here an example of how what you are trying to do is possible with the IF-Node and the Function-Node:
(you can simply copy&paste it directly into n8n, it will then create all nodes and connections)

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    \"json\": {\n      \"UserName\": \"[email protected]\",\n      \"Password\": \"Xyz-123\"\n    }\n  },\n  {\n    \"json\":   {\n      \"UserName\": \"[email protected]\", \n      \"Password\": \"Xyz-123\"\n    },\n  },\n  {\n    \"json\": {\n      \"UserName\": \"[email protected]\",\n      \"Password\": \"Xyz-123\"\n    }\n  }\n];"
      },
      "name": "Create Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        400
      ]
    },
    {
      "parameters": {
        "functionCode": "return items.filter(item => {\n  return item.json.UserName.toLowerCase().includes('john')\n})"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        800,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [],
          "boolean": [
            {
              "value1": true,
              "value2": "={{$node[\"Create Data\"].data[\"UserName\"].toLowerCase().includes('john')}}"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        800,
        500
      ]
    }
  ],
  "connections": {
    "Create Data": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          },
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
[
    [
        {
            "name": "john",
            "city": "detroit",
            "phone": "012345678",
        },
        {
            "name": "ted",
            "city": "chicago",
            "phone": "012345678",
        },
        {
            "name": "emma",
            "city": "LA",
            "phone": "012345678",
        }
    ]
]

Hi jan, I am getting a http response in this format, could you tell me how can I get the json with “name” “emma” using function node.

var new_obj = items[0];

var filtered = new_obj.filter(i =>{
    return i.name.toLowerCase().includes("emma")
});


return filtered;

This is the code i am running in function node.
I am getting:

ERROR: new_obj.filter is not a function

You should probably check again the documentation about the Function-Node which I did post earlier. That one also links to a page about the internal data structure used which is important when the Function-Node does get used. That documentation can be found here:
https://docs.n8n.io/#/data-structure
To use the Function-Node it is very important to understand how the data which flows through the workflow looks exactly.

What was missing is that the JSON-data of each item is saved in a property named “json”. So the data has to get read from it and also does the data have to get returned in it. Hope that makes sense.

Bellow the fixed version of your code:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "items[0].json = [\n        {\n            \"name\": \"john\",\n            \"city\": \"detroit\",\n            \"phone\": \"012345678\",\n        },\n        {\n            \"name\": \"ted\",\n            \"city\": \"chicago\",\n            \"phone\": \"012345678\",\n        },\n        {\n            \"name\": \"emma\",\n            \"city\": \"LA\",\n            \"phone\": \"012345678\",\n        }\n];\nreturn items;"
      },
      "name": "Create Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        550,
        650
      ]
    },
    {
      "parameters": {
        "functionCode": "var new_obj = items[0].json;\n\nvar filtered = new_obj.filter(item => {\n    return item.name.toLowerCase().includes(\"emma\")\n}).map(item => {\n  return { json: item };\n});\n\n\nreturn filtered;\n"
      },
      "name": "Filter Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        750,
        650
      ]
    }
  ],
  "connections": {
    "Create Data": {
      "main": [
        [
          {
            "node": "Filter Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thanks Jan, that cleared my doubt.

No problem. Always happy to help! Have a great day!

1 Like