How to filter json data using function node?

Hi everyone I want to ask
I have json data like this:

[
    {
        "data": [
            {
                "id": "01",
                "name": "name 1",
                "email": "[email protected]",
                "created_at": "2020-08-31T03:52:30",
                "user_roles": [
                    {
                        "id": "111",
                        "name": "General"
                    }
                ]
            },
            {
                "id": "02",
                "name": "name 2",
                "email": "[email protected]",
                "created_at": "2020-08-31T03:52:30",
                "user_roles": [
                    {
                        "id": "111",
                        "name": "General"
                    }
                ]
            },
            {
                "id": "03",
                "name": "name 3",
                "email": "[email protected]",
                "created_at": "2020-08-31T03:52:30",
                "user_roles": [
                    {
                        "id": "222",
                        "name": "Supervisor"
                    }
                ]
            }
        ]
    }
]

How to filter the json data based on the user’s role is General?
So the results I will get like this

[
    {
        "data": [
            {
                "id": "01",
                "name": "name 1",
                "email": "[email protected]",
                "created_at": "2020-08-31T03:52:30",
                "user_roles": [
                    {
                        "id": "111",
                        "name": "General"
                    }
                ]
            },
            {
                "id": "02",
                "name": "name 2",
                "email": "[email protected]",
                "created_at": "2020-08-31T03:52:30",
                "user_roles": [
                    {
                        "id": "111",
                        "name": "General"
                    }
                ]
            }
        ]
    }
]

Thank you very much for your answer

Welcome to the community @suparman_aguuus!

There are multiple ways to do that. Really depends on what you want to do with the data afterward and what else you have planned.

One way would be to move the data you have underneath data into different items and then filter via an IF-Node.
Another way would be to directly filter the data with the help of a Function-Node and leave it all in the first item underneath data.

Here the workflow for both:

3 Likes

Ah cool, my problem is solved thank you very much @jan

Happy to hear! Have fun!

Hello, what if within this script I want to filter by complying to values (OR).

I guess that could be like this, but doesn’t work…

items[0].json.data = items[0].json.data.filter(item => {
  return item.user_roles[0].name === ('General' || "Supervisor");
})

return items;

I know that in this example doesn’t have any sense but I need to use on my flow and didn’t know how.

Thanks in advance!