# Test Body response by looping through Array or If/Else

**URL:** <https://community.postman.com/t/test-body-response-by-looping-through-array-or-if-else/46173>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [April 10, 2023, 7:51pm UTC](https://community.postman.com/t/test-body-response-by-looping-through-array-or-if-else/46173 "2023-04-10T19:51:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![qateamll](https://avatars.discourse-cdn.com/v4/letter/q/85f322/32.png) [@qateamll](https://community.postman.com/u/qateamll)\
**Post date:** [April 10, 2023, 7:51pm UTC](https://community.postman.com/t/test-body-response-by-looping-through-array-or-if-else/46173/1 "2023-04-10T19:51:32Z")

</div>

New to Postman and starting to get into more complex testing base on the body responses. My aim is to test several arrays of data from the body response.

Questions is how best to either loop through testing or by doing if/else or try catch conditions when the data has the same properties but different values in each separate array of data?

Example: AccountID shows in all arrays but the numbers obviously change in each. I want to account for those to not be null. I know I can do a pm.expect (json.data.AccountID).to.be.not.null but I was looking for more efficient way to loop through and conditionally check each array output? Maybe I’m making this harder than it needs to be??

{  
“AccountID”: 123485,  
“AssociateNumber”: 123456,  
“AssociateType”: “Account”,  
“AccountType”: “Business”,  
“DisplayName”: “Test”,  
“FirstResDate”: “2011-07-06T00:00:00”,  
“LastResDate”: “2011-09-14T00:00:00”,  
“LastContactDate”: “2010-12-26T00:00:00”,  
“PricingLevel”: “Executive”,  
“ActiveFlag”: false,  
“CreatedBy”: “Conversion”,  
“CreatedDate”: “2010-12-26T07:48:34.827”,  
“ModifiedBy”: “Test”,  
“ModifiedDate”: “2020-09-02T07:27:29”  
},  
}, {  
“AccountID”: 123485,  
“AssociateNumber”: 123456,  
“AssociateType”: “Account”,  
“AccountType”: “Business”,  
“DisplayName”: “Test”,  
“FirstResDate”: “2011-07-06T00:00:00”,  
“LastResDate”: “2011-09-14T00:00:00”,  
“LastContactDate”: “2010-12-26T00:00:00”,  
“PricingLevel”: “Executive”,  
“ActiveFlag”: false,  
“CreatedBy”: “Conversion”,  
“CreatedDate”: “2010-12-26T07:48:34.827”,  
“ModifiedBy”: “Test”,  
“ModifiedDate”: “2020-09-02T07:27:29”  
},

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [April 11, 2023, 7:16am UTC](https://community.postman.com/t/test-body-response-by-looping-through-array-or-if-else/46173/2 "2023-04-11T07:16:46Z")

</div>

Hi @qateamll

When I was first learning Postman [I created this workspace with my examples](https://www.postman.com/dark-matter-505283/workspace/code-snippets/collection/23596470-7584dd11-410f-47bc-804d-534140aceef7?action=share&creator=22617629).

It should have everything you are asking for under the “Test Script Examples” collection.

---

<div class="post-metadata">

**Author:** ![qateamll](https://avatars.discourse-cdn.com/v4/letter/q/85f322/32.png) [@qateamll](https://community.postman.com/u/qateamll)\
**Post date:** [April 11, 2023, 11:07am UTC](https://community.postman.com/t/test-body-response-by-looping-through-array-or-if-else/46173/3 "2023-04-11T11:07:08Z")

</div>

Great, thank you! I will focus on that area for solutions…

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [April 11, 2023, 11:31am UTC](https://community.postman.com/t/test-body-response-by-looping-through-array-or-if-else/46173/4 "2023-04-11T11:31:36Z")

</div>

Here is an example. It’s uses backticks\string literals to customize the test case name, which is fairly important when looping through a data set to know which one is actually failing.

```auto
const array = pm.response.json(); 

pm.test("Array check", () => {
    pm.expect(array).to.be.an("array").that.is.not.empty;
});

array.forEach(element => {
    pm.test(`expect ${element.AccountID} to not be null`, () => {
        pm.expect(element.AccountID).is.not.oneOf([null, undefined]);
    });
});

```

---

<div class="post-metadata">

**Author:** ![qateamll](https://avatars.discourse-cdn.com/v4/letter/q/85f322/32.png) [@qateamll](https://community.postman.com/u/qateamll)\
**Post date:** [April 11, 2023, 11:47am UTC](https://community.postman.com/t/test-body-response-by-looping-through-array-or-if-else/46173/5 "2023-04-11T11:47:13Z")

</div>

Thanks very much. Between your example and the examples from w4dd325 this is what I was hoping for and a way I can continue learning and building from these. I really appreciate the direction here which will assist me in moving forward with a little nudge 🙂
