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:

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)

1 Like
[
    [
        {
            "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:

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:

Thanks Jan, that cleared my doubt.

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

1 Like