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": "email1.@mail.com",
                "created_at": "2020-08-31T03:52:30",
                "user_roles": [
                    {
                        "id": "111",
                        "name": "General"
                    }
                ]
            },
            {
                "id": "02",
                "name": "name 2",
                "email": "email2.@mail.com",
                "created_at": "2020-08-31T03:52:30",
                "user_roles": [
                    {
                        "id": "111",
                        "name": "General"
                    }
                ]
            },
            {
                "id": "03",
                "name": "name 3",
                "email": "email3.@mail.com",
                "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": "email1.@mail.com",
                "created_at": "2020-08-31T03:52:30",
                "user_roles": [
                    {
                        "id": "111",
                        "name": "General"
                    }
                ]
            },
            {
                "id": "02",
                "name": "name 2",
                "email": "email2.@mail.com",
                "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:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "items[0].json.data = [\n            {\n                \"id\": \"01\",\n                \"name\": \"name 1\",\n                \"email\": \"email1.@mail.com\",\n                \"created_at\": \"2020-08-31T03:52:30\",\n                \"user_roles\": [\n                    {\n                        \"id\": \"111\",\n                        \"name\": \"General\"\n                    }\n                ]\n            },\n            {\n                \"id\": \"02\",\n                \"name\": \"name 2\",\n                \"email\": \"email2.@mail.com\",\n                \"created_at\": \"2020-08-31T03:52:30\",\n                \"user_roles\": [\n                    {\n                        \"id\": \"111\",\n                        \"name\": \"General\"\n                    }\n                ]\n            },\n            {\n                \"id\": \"03\",\n                \"name\": \"name 3\",\n                \"email\": \"email3.@mail.com\",\n                \"created_at\": \"2020-08-31T03:52:30\",\n                \"user_roles\": [\n                    {\n                        \"id\": \"222\",\n                        \"name\": \"Supervisor\"\n                    }\n                ]\n            }\n        ];\nreturn items;"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"user_roles\"][0][\"name\"]}}",
              "value2": "General"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {},
      "name": "General",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1050,
        200
      ]
    },
    {
      "parameters": {},
      "name": "Others",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1050,
        350
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.data.map(item => {\n  return {\n    json: item,\n  }\n})"
      },
      "name": "Split into Items",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "items[0].json.data = items[0].json.data.filter(item => {\n  return item.user_roles[0].name === 'General';\n})\n\nreturn items;"
      },
      "name": "Filter",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        550
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Split into Items",
            "type": "main",
            "index": 0
          },
          {
            "node": "Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "General",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Others",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split into Items": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

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

Happy to hear! Have fun!