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

**URL:** <https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017>\
**Category:** Help Hub\
**Tags:** tests, responseassertion, array\
**Created:** [January 10, 2020, 7:25pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017 "2020-01-10T19:25:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![LearningPostmanR](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LearningPostmanR](https://community.postman.com/u/LearningPostmanR)\
**Post date:** [January 10, 2020, 7:25pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/1 "2020-01-10T19:25:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 10, 2020, 9:22pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/2 "2020-01-10T21:22:41Z")

</div>

Hey @LearningPostmanR,

Welcome to the community! 🚀

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.

---

<div class="post-metadata">

**Author:** ![LearningPostmanR](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LearningPostmanR](https://community.postman.com/u/LearningPostmanR)\
**Post date:** [January 13, 2020, 2:51pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/3 "2020-01-13T14:51:36Z")

</div>

Yayyii! Thankyou so much! Worked just fine 🙂

---

<div class="post-metadata">

**Author:** ![rohit2330](https://avatars.discourse-cdn.com/v4/letter/r/65b543/32.png) [@rohit2330](https://community.postman.com/u/rohit2330)\
**Post date:** [August 6, 2020, 9:27am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/4 "2020-08-06T09:27:35Z")

</div>

Hey @danny-dainton,

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.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [August 6, 2020, 10:39am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/5 "2020-08-06T10:39:51Z")

</div>

What have you tried so far?

What’s not working? 😁

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.

---

<div class="post-metadata">

**Author:** ![rohit2330](https://avatars.discourse-cdn.com/v4/letter/r/65b543/32.png) [@rohit2330](https://community.postman.com/u/rohit2330)\
**Post date:** [August 6, 2020, 1:21pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/7 "2020-08-06T13:21:56Z")

</div>

Thanks for these details @danny-dainton This worked for me.

---

<div class="post-metadata">

**Author:** ![achavate](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@achavate](https://community.postman.com/u/achavate)\
**Post date:** [September 9, 2020, 12:11am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/8 "2020-09-09T00:11:20Z")

</div>

I have an Array that looks like this. the FilterId is auto-generated from a PUT request.

Am storing this filterId in an 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?

```auto
[
    {
        "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": ""
            }
        ]

```

---

<div class="post-metadata">

**Author:** ![BelairDA](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@BelairDA](https://community.postman.com/u/BelairDA)\
**Post date:** [November 19, 2020, 10:54pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/9 "2020-11-19T22:54:56Z")

</div>

I have the exact same situation, did you find an answer ?

---

<div class="post-metadata">

**Author:** ![vivekRESTs](https://avatars.discourse-cdn.com/v4/letter/v/c6cbf5/32.png) [@vivekRESTs](https://community.postman.com/u/vivekRESTs)\
**Post date:** [April 7, 2021, 1:14pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/10 "2021-04-07T13:14:26Z")

</div>

Hi @danny-dainton ,  
I have a similar situation where I need to validate the Fee is “123” for the clientName - “Seville”. How do I accomplish that? Should I go the for loop route? I am confused and your help would be great.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 7, 2021, 1:41pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/11 "2021-04-07T13:41:52Z")

</div>

You could just do a `.find()` on the array and return the object which matches the condition.

From there do your assertion on the `Fee` property to check it matches the value you expect.

```auto
pm.test('Check the Fee for Seville', () => {
    let client = pm.response.json().Fee.find(a => a.clientName === 'Seville');
    pm.expect(client.Fee).to.equal("123");
});

```

---

<div class="post-metadata">

**Author:** ![vivekRESTs](https://avatars.discourse-cdn.com/v4/letter/v/c6cbf5/32.png) [@vivekRESTs](https://community.postman.com/u/vivekRESTs)\
**Post date:** [April 7, 2021, 1:55pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/12 "2021-04-07T13:55:04Z")

</div>

Thank you so much Danny. It’s like seeing the nose all the time but gets ignored by the brain. 🤦‍♂️

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 7, 2021, 2:27pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/13 "2021-04-07T14:27:21Z")

</div>

Sorry @achavate I didn’t see this reply but if it’s still an issue, you could do something like this I guess:

```auto
pm.test(`filterId contains ${pm.environment.get('filterIdFromPutRequest')}`, () => {
    let filterIdFromPutRequest = parseInt(pm.environment.get('filterIdFromPutRequest')),
        id = pm.response.json().filter(obj => obj.filterId === filterIdFromPutRequest);
        
    pm.expect(id[0].filterId).to.eql(filterIdFromPutRequest)
})

```

There are probably more efficient ways to achieve the same thing though. 😄

---

<div class="post-metadata">

**Author:** ![jagrutiwani0](https://avatars.discourse-cdn.com/v4/letter/j/7ab992/32.png) [@jagrutiwani0](https://community.postman.com/u/jagrutiwani0)\
**Post date:** [April 14, 2021, 1:01pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/14 "2021-04-14T13:01:15Z")

</div>

Hi @danny-dainton

I have this response body-

```auto
{
    "Responses": [
        {
            "WT_Status": "Success"
        },
        {
            "NS_Status": "Success"
        }
    ]
}

```

I wanted to verify if **NS\_Status** is **success** or not.

I tried using the below code:

```auto
let jsonData = pm.response.json()
 pm.test("Response body contains", () => {
     _.each(jsonData.Responses, (item) => {
         pm.expect(item.NS_Status).to.include('Success')
     })
 });

```

I’m getting an error saying **Response body contains | AssertionError: object tested must be an array, a map, an object, a set, a string, or a weakset, but undefined given**

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 14, 2021, 1:37pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/15 "2021-04-14T13:37:14Z")

</div>

What the test is doing, is running through _each_ of the objects in the `Responses` array and checking the `NS_Status` for the value.

The problem with that approach is that `NS_Status` isn’t part of the first object and would be `undefined`, which is what the error message is telling you here.

> Response body contains | AssertionError: object tested must be an array, a map, an object, a set, a string, or a weakset, but undefined given

You could use `.filter()` like some of the thread messages or maybe `.find()`:

```auto
let jsonData = pm.response.json()
pm.test("Response body contains", () => {
    let result = jsonData.Responses.find(a => a.NS_Status === "Success")
    pm.expect(result.NS_Status).to.eql("Success")
});

```

I don’t have a lot of context for your specific use case so all I can offer is possible solutions, to get you closer to something that works for you.

---

<div class="post-metadata">

**Author:** ![jagrutiwani0](https://avatars.discourse-cdn.com/v4/letter/j/7ab992/32.png) [@jagrutiwani0](https://community.postman.com/u/jagrutiwani0)\
**Post date:** [April 15, 2021, 7:49am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/16 "2021-04-15T07:49:26Z")

</div>

Thank you so much 🙂 It is working now.

---

<div class="post-metadata">

**Author:** ![sariknpl](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@sariknpl](https://community.postman.com/u/sariknpl)\
**Post date:** [June 9, 2021, 3:24pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/17 "2021-06-09T15:24:06Z")

</div>

Hi @danny-dainton

I have a situation where I need to validate a specific error is present in the Json response.

For example, in the below Json response, I need to verify that **“errorMessage” : “My Error”** is present in the response.

However, it can be anywhere in the array and not in a particular index. So I just need to verify it’s present regardless of what index it is showing up at.

Without writing a complex loop, is there an easy postman feature I can use to test this? Thank You!

```auto
[
   {
      "id":"sampleId",
      "errors":[
         {
            "errorCode":"Validation Error",
            "errorMessage":"My Error"
         },
         {
            "errorCode":"Validation error",
            "errorMessage":"Your Error"
         },
         {
            "errorCode":"Validation error",
            "errorMessage":"His Error"
         }
      ]
   }
]

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [June 9, 2021, 3:41pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/18 "2021-06-09T15:41:26Z")

</div>

Something like this would give you a `true/false` response if the error message is part of the `errors` array:

```javascript
console.log(pm.response.json()[0].errors.some(a => a.errorMessage === "My Error"))

```

That could be made into a quick test like this:

```javascript
let isErrorPresent = pm.response.json()[0].errors.some(a => a.errorMessage === "My Error");

pm.test("Verify error message", () => {
    pm.expect(isErrorPresent, "Error message not present in the array").to.be.true;
});

```

---

<div class="post-metadata">

**Author:** ![sariknpl](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@sariknpl](https://community.postman.com/u/sariknpl)\
**Post date:** [June 9, 2021, 4:04pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/19 "2021-06-09T16:04:17Z")

</div>

Thanks Danny,

This was really helpful.

---

<div class="post-metadata">

**Author:** ![ganeshsurya0589](https://avatars.discourse-cdn.com/v4/letter/g/7ab992/32.png) [@ganeshsurya0589](https://community.postman.com/u/ganeshsurya0589)\
**Post date:** [June 22, 2021, 10:39am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/20 "2021-06-22T10:39:13Z")

</div>

Hi @danny-dainton  
I have a situation where I need to validate each object in array contains clientID irrespective of value in the Json response.  
Can you please help me in writing this assertion? Thanks…!!

```auto
[
    {
       
        "person": {
            "srNo": "123",
                       
        },
        
    },
    {
         "person": {
            "srNo": "234",
                       
        },
    },
    {
       "person": {
            "srNo": "234",
                       
        },
    }
]

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [June 22, 2021, 10:54am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/21 "2021-06-22T10:54:23Z")

</div>

What have you tried so far? What’s not currently working for you?

[Next page](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017.md?page=2)
