To assert that the array in the response body contains a string value in all its elements

Hi,

New to Postman and not accustomed to writing code so please help. Couldn’t find any similar post either which I could use.
I want to add an assertion to check whether a value is present in all the elements in an array.

In the below example, I have an array Fee with 3 elements.
I want to check whether all of them have a ClientName which contains “Seville” in it. How could I do that?
{
“Fee”: [
{
“Id”: 1,
“clientName”: “Seville ABC”,
“ProductName”: “Product A”,
“Fee”: “123”,
},
{
“Id”: 2,
“clientName”: “Seville”,
“ProductName”: “Product A”,
“Fee”: “123”,
},
{
“Id”: 3,
“clientName”: “Seville XYZ”,
“ProductName”: “Product A”,
“Fee”: “123”,
}
]

Thanks in advance.

Hey @LearningPostmanR,

Welcome to the community! :rocket:

You could try something like this:

let jsonData = pm.response.json()

pm.test('The `clientName` contains Seville', () => {
    _.each(jsonData.Fee, (item) => {
        pm.expect(item.clientName).to.include('Seville')
    })
})

It’s looping through the Fee array and then using the .include chai function the check if the clientName value, of each object, contains that string.

1 Like

Yayyii! Thankyou so much! Worked just fine :slight_smile:

1 Like