How to filter response based on specific custom field?

Thanks, everyone.

When I get the response with this API https://reqres.in/api/users?page=2

I could see the response as below, but I would like to filter the data if email = “lindsay.ferguson@reqres.in”, could someone help here?

{
    "page": 2,
    "per_page": 6,
    "total": 12,
    "total_pages": 2,
    "data": [
        {
            "id": 7,
            "email": "michael.lawson@reqres.in",
            "first_name": "Michael",
            "last_name": "Lawson",
            "avatar": 
        },
        {
            "id": 8,
            "email": "lindsay.ferguson@reqres.in",
            "first_name": "Lindsay",
            "last_name": "Ferguson",
            "avatar": 
        },
        {
            "id": 9,
            "email": "tobias.funke@reqres.in",
            "first_name": "Tobias",
            "last_name": "Funke",
            "avatar": 
        },
        {
            "id": 10,
            "email": "byron.fields@reqres.in",
            "first_name": "Byron",
            "last_name": "Fields",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/russoedu/128.jpg"
        },
        {
            "id": 11,
            "email": "george.edwards@reqres.in",
            "first_name": "George",
            "last_name": "Edwards",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/mrmoiree/128.jpg"
        },
        {
            "id": 12,
            "email": "rachel.howell@reqres.in",
            "first_name": "Rachel",
            "last_name": "Howell",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg"
        }
    ]
}

Hey @Venkat.batchu81

It doesn’t look like that endpoint offers the ability to filter down the response, some endpoints would offer that capability via URL params. It looks like they have this for some items like page but not much else.

Depending on your use case you can still do this in a couple of ways with Postman. In the Tests tab, you can loop through the objects in the data array and log the one that contains the email address to the console

_.each(pm.response.json().data, (item) => {
    if(item.email === 'lindsay.ferguson@reqres.in') {
        console.log(item)
    }
})

I’ve also created a template, which can be imported directly into your Postman app, that uses the jsonpath syntax in the Visualizer to filter down the response data.

This would be something that you could use to filter down to an object containing a specific email address:

$..data[?(@.email=='lindsay.ferguson@reqres.in')]

2 Likes

Thanks @dannydainton , its working perfectly.

1 Like