# 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:** 12\
**Page:** 2

<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, 11:46am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/22 "2021-06-22T11:46:10Z")

</div>

```auto
let jsonData = pm.response.json()

pm.test('The `srNo` in each record' , () => {
    _.each(jsonData.srNo, (item) => {
        pm.expect(item.srNo)
    })
})

```

//This is what I Tried not going well  
//Thanks for the Reply

---

<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, 12: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/23 "2021-06-22T12:37:05Z")

</div>

There are a few ways that you could do this - Sticking to the direction that you were heading in, this could work:

```auto
let jsonData = pm.response.json();

pm.test('The `srNo` property is in each record' , () => {
    _.each(jsonData, (item) => {
        pm.expect(item.person, 'Does not have the property').to.have.property('srNo');
    });
});

```

---

<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, 12:57pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/24 "2021-06-22T12:57:29Z")

</div>

Hey,  
Thanks for the suggestion, It’s working fine now.  
again Thank you

---

<div class="post-metadata">

**Author:** ![arunece09](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@arunece09](https://community.postman.com/u/arunece09)\
**Post date:** [June 23, 2021, 1:53am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/25 "2021-06-23T01:53:27Z")

</div>

I am new to Postman. I want to check `Id=3`, I have tried like below:

```auto
pm.test('The `Id` contains 3', () => {
    _.each(jsonData.Fee, (item) => {
        pm.expect(item.Id).to.include(1)
    })
})

```

Actually, it is giving an assertion error. `expected 2 to include 1`

---

<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 23, 2021, 9:21am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/26 "2021-06-23T09:21:01Z")

</div>

Hey @arunece09

I’m not sure what it is you’re trying to assert against, you might need to share the response body too.

The code is looping through the data so it will check each object and if all the object `Id` properties are not `1` then it would fail like you can see.

---

<div class="post-metadata">

**Author:** ![chintha992](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@chintha992](https://community.postman.com/u/chintha992)\
**Post date:** [June 23, 2021, 11:53am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/28 "2021-06-23T11:53:10Z")

</div>

This is from another script, Hope you can make this together.

```auto
pm.test("The response has all properties.", () => 
{
    pm.expect(jsonData).to.be.an("array");
    pm.expect(jsonData[0].channel.id).to.be.a("string");
    pm.expect(jsonData[0].postList).to.be.an("array");
});
```

---

<div class="post-metadata">

**Author:** ![mw49](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@mw49](https://community.postman.com/u/mw49)\
**Post date:** [June 23, 2021, 2:02pm UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/29 "2021-06-23T14:02:45Z")

</div>

Hey @danny-dainton

I want to ask you about this request

```auto
pm.test('The "Number_1" contains ', () => {
    _.each(jsonData.Fee, (item) => {
        pm.expect(item.Number_1).to.include('+33 ')
    })
})

```

But it didn’t work unfortunately when i searched throw 2 arrays like this i want to get except for the “Number\_1” and “Number\_2”

```auto
{
   "Client_Data":{
      "User":{
         "Name":"Juan",
         "Surname":"Pablo",
         "Birthday":"12.07.1991",
         "Contact":{
            "Telefon":[
               {
                  "Number_1":"+33 6084986932",
                  "Number_2":"+33 4596127896"
               }
            ]
         }
      }
   }
}

```

---

<div class="post-metadata">

**Author:** ![arunece09](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@arunece09](https://community.postman.com/u/arunece09)\
**Post date:** [June 24, 2021, 12:40am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/30 "2021-06-24T00:40:36Z")

</div>

Thank you so much for your quick response. Here is my response body.

```auto
{
   "data":[
      {
         "id":1,
         "email":"george.bluth@reqres.in",
         "first_name":"George",
         "last_name":"Bluth",
         "avatar":"https://reqres.in/img/faces/1-image.jpg"
      },
      {
         "id":2,
         "email":"janet.weaver@reqres.in",
         "first_name":"Janet",
         "last_name":"Weaver",
         "avatar":"https://reqres.in/img/faces/2-image.jpg"
      }
   ]
}

```

Below is my Tests, where I need to check email is `george.bluth@reqres.in` present in the response body.

```auto
let JsonData= pm.response.json();

pm.test("Verify email", () => {
    _.each(JsonData.data, (item) => {
        pm.expect(item.email).to.include('george.bluth@reqres.in')
    })
})

```

it is giving the Assertion error: expected `janet.weaver@reqres.in` to include `george.bluth@reqres.in`

---

<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 24, 2021, 11:25am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/31 "2021-06-24T11:25:10Z")

</div>

Hey @mw49

As you’re copying the code from a completely different response body, all the references you’re using are not correct.

You could use _something_ like this to check the values of the items in that single object, with the array. It’s using `.match` to check the value.

This is a good site to get more understanding of how regex works - [regex101: build, test, and debug regex](https://regex101.com/#javascript)

As you’re looking at all the values in that object, you would need to get them before you can loop through them, that’s why i’m using `_.values` to get them from the object. You can see what values are captured by uncommenting the `console.log` statement.

```auto
let jsonData = pm.response.json();

pm.test('The numbers contain +33 ', () => {
    let phoneNumbers = _.values(jsonData['Client_Data'].User.Contact.Telefon[0]);
    // console.log(phoneNumbers);
    _.each(phoneNumbers, (number) => {
        pm.expect(number).to.match(/\+(33)\s\d+/);
    });
});

```

---

<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 24, 2021, 11:34am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/32 "2021-06-24T11:34:55Z")

</div>

As you’re using a loop, it’s looking at each of the `email` properties and checking if it includes that value. As the second object doesn’t include that, it’s failing.

This would target that specific value in the first object:

```auto
pm.test("Verify email", () => {
   pm.expect(jsonData.data[0].email).to.eql('george.bluth@reqres.in')
})

```

You could use something like this to check them all and give a `true/false` if it’s present:

```auto
pm.test("Verify email", () => {
   pm.expect(jsonData.data.some(a => a.email === 'george.bluth@reqres.in'), 'Email not in array').to.be.true;
})

```

---

<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 24, 2021, 11:40am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/33 "2021-06-24T11:40:12Z")

</div>

I can see that many people are asking follow up questions on this thread that don’t relate to the original question.

I’m going to close this topic and ask that you create a new topic that contains all the details for your particular question, in your own context.

It will save others on the thread receiving notifications for non-related questions. 😃

---

<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 24, 2021, 11:40am UTC](https://community.postman.com/t/to-assert-that-the-array-in-the-response-body-contains-a-string-value-in-all-its-elements/10017/34 "2021-06-24T11:40:23Z")

</div>

Closed on June 24, 2021, 11:40am UTC



[Previous 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=1)
