Filter JSON response

While working with JSON, you might have come across scenarios where you want to filter the data before binding it to controls or using it for some other purpose. This use case demonstrates how to apply filter upon JSON data using custom JS Code.

This article makes use of JSON data received from an API.

JSON Data (Raw Response)

{
  "result": {
    "rows": [
      {
        "ID": 18,
        "FirstName": "Tomasa",
        "LastName": "Hermann",
        "Address": "751 Koss Forest",
        "Email": "tomasa.hermann41@example.com",
        "Designation": "Intern"
      },
      {
        "ID": 24,
        "FirstName": "Cedrick",
        "LastName": "Schmeler",
        "Address": "695 Rau Passage",
        "Email": "cedrick89@example.com",
        "Designation": "Workspace Admin"
      },
      {
        "ID": 36,
        "FirstName": "Timmy",
        "LastName": "Schamberger",
        "Address": "24593 Bayer Oval",
        "Email": "timmy_schamberger@example.com",
        "Designation": "Workspace Manager"
      },
      {
        "ID": 1,
        "FirstName": "Carlie",
        "LastName": "Schmeler",
        "Address": "2375 Allen Flats",
        "Email": "carlie43@example.com",
        "Designation": "Automation Tester"
      },
      {
        "ID": 2,
        "FirstName": "Katrina",
        "LastName": "Mosciski",
        "Address": "2570 Toy Lights",
        "Email": "katrina94@example.com",
        "Designation": "Software Developer"
      },
      {
        "ID": 3,
        "FirstName": "Lurline",
        "LastName": "Powlowski",
        "Address": "551 Morar Expressway",
        "Email": "lurline90@example.com",
        "Designation": "Market Intern"
      },
      {
        "ID": 11,
        "FirstName": "Micah",
        "LastName": "Mosciski",
        "Address": "5184 Keely Trafficway",
        "Email": "micah.mosciski65@example.com",
        "Designation": "Intern"
      }
    ]
  }
}

In the connectors, transform response section, we can write JS Code to filter the JSON response to search for some specific values or strings. The below code filters data variable to have only those records where the designation field contains “Intern” word.

Code

function filterDesignation(record) {
    return record.Designation.includes('Intern');
}
data = data.result.rows.filter(filterDesignation);

 
In the above code, the filter value Intern is hard-coded, you can also use some sort of dropdown value to filter the JSON data as shown below.

JSON Data (Transformed Response)

[
  {
    "ID": 18,
    "FirstName": "Tomasa",
    "LastName": "Hermann",
    "Address": "751 Koss Forest",
    "Email": "tomasa.hermann41@example.com",
    "Designation": "Intern"
  },
  {
    "ID": 3,
    "FirstName": "Lurline",
    "LastName": "Powlowski",
    "Address": "551 Morar Expressway",
    "Email": "lurline90@example.com",
    "Designation": "Market Intern"
  },
  {
    "ID": 11,
    "FirstName": "Micah",
    "LastName": "Mosciski",
    "Address": "5184 Keely Trafficway",
    "Email": "micah.mosciski65@example.com",
    "Designation": "Intern"
  }
]

Initial Data (Unfiltered)

Filtered Data (records which contain Intern as Designation)