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.

2 Likes

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

1 Like

Hey @dannydainton,

I have similar kind of requirement, I need assertion which will check clientName starts with string Seville or seville(s is in smaller case). Can you please help me with this please.

What have you tried so far?

What’s not working? :grin:

Using the text above you could modify the expect statement to add in an OR condition.

pm.test('The `clientName` starts With Seville or seville', () => {
    _.each(jsonData.Fee, (item) => {
        pm.expect(item.clientName.startsWith("Seville") || item.clientName.startsWith("seville")).to.be.true
    })
})

You could also do something with regex and check for case insensitive words or even convert the string to upper or lower case to check the word - I don’t really know your use case so it’s hard for me to say.

1 Like

Thanks for these details @dannydainton This worked for me.

I have a Array which looks like this. the FilterId is auto generated from a PUT request.
Am storing this filterId in a environment variable filterIdFromPutRequest.
How can I use the code above to validate that the FilerID which was generated from the PUT exists in the GET which is executed After the PUT

[
{
“filterId”: 326994,
“description”: “3filters1”,
“details”: [
{
“name”: “cc1”,
“value”: “”
},
{
“name”: “cc2”,
“value”: “”
}

    ]
},
{
    "filterId": 326997,
    "description": "3filters1_edit",
    "details": [
        {
            "name": "cc1",
            "value": ""
        }
    ]
},
{
    "filterId": 326999,
    "description": "Auto_APISavedSearch",
    "details": [
        {
            "name": "cc1",
            "value": ""
        }
                  
   

    ]