# How to do a assertion on a nested JSON array?

**URL:** <https://community.postman.com/t/how-to-do-a-assertion-on-a-nested-json-array/51261>\
**Category:** Help Hub\
**Tags:** tests, scripts, response\
**Created:** [September 1, 2023, 1:35pm UTC](https://community.postman.com/t/how-to-do-a-assertion-on-a-nested-json-array/51261 "2023-09-01T13:35:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![omernauman](https://avatars.discourse-cdn.com/v4/letter/o/f05b48/32.png) [@omernauman](https://community.postman.com/u/omernauman)\
**Post date:** [September 1, 2023, 1:35pm UTC](https://community.postman.com/t/how-to-do-a-assertion-on-a-nested-json-array/51261/1 "2023-09-01T13:35:30Z")

</div>

Hi,

I am looking for assertion where test should fail, if one of businessConditionStatus is false.  
This is how response looks like.

```auto
{
    "response": {
        "entities": [
            {
                "id": "d8410b29b305",
                "type": "SKU",
                "data": {
                    "attributes": {
                        "businessConditions": {
                            "group": [                             
                                {
                                    "businessConditionName": {
                                        "values": [
                                            {
                                                "value": "ConditionA"
                                            }
                                        ]
                                    },
                                    "businessConditionStatus": {
                                        "values": [
                                            {
                                                "value": true
                                            }
                                        ]
                                    }
                                },
                                {
                                    "businessConditionName": {
                                        "values": [
                                            {
                                                "value": "ConditionB"
                                            }
                                        ]
                                    },
                                    "businessConditionStatus": {
                                        "values": [
                                            {
                                                "value": false
                                            }
                                        ]
                                    }
                                }                              
                            ]
                        }
                    }
                }
            }
        ]
    }
}

```

---

<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:** [September 1, 2023, 2:26pm UTC](https://community.postman.com/t/how-to-do-a-assertion-on-a-nested-json-array/51261/2 "2023-09-01T14:26:39Z")

</div>

Hey @omernauman 👋

Welcome to the Postman community! :postman:

What have you tried so far?

We have a lot of content within our learning center that will help you out with this problem.

> **[Test script examples | Postman Learning Center](https://learning.postman.com/docs/writing-scripts/script-references/test-examples/)**
>
> Test script examples: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

---

<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:** [September 1, 2023, 2:44pm UTC](https://community.postman.com/t/how-to-do-a-assertion-on-a-nested-json-array/51261/3 "2023-09-01T14:44:50Z")

</div>

Following on from Danny’s advice to look through the examples first.

I can think of two ways of doing this.

1. Loop though the response and test each one individually. If you use string literals in the test case name, you can customize the test case name so its more specific when one fails.

2. If you don’t really care which one fails, only that there are no “false” values, then you might want to consider a JavaScript filter command to filter for false values, and the test would just be a case of checking that the variable you assign the results of the filter is “null”. (Which means it didn’t find any entries).

---

<div class="post-metadata">

**Author:** ![omernauman](https://avatars.discourse-cdn.com/v4/letter/o/f05b48/32.png) [@omernauman](https://community.postman.com/u/omernauman)\
**Post date:** [September 1, 2023, 4:13pm UTC](https://community.postman.com/t/how-to-do-a-assertion-on-a-nested-json-array/51261/4 "2023-09-01T16:13:03Z")

</div>

thanks for sharing the link. this is what i have tried so far:

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

let id = jsonData.response.entities[0].id;
let attributes = Object.keys(jsonData.response.entities[0].data.attributes.businessConditions.group);
let obj = jsonData.response.entities[0].data.attributes.businessConditions.group;

pm.test(`Has user input | ID: ${id}`, () => {
    attributes.map(x => {
        console.info(`Has user input | Arr size: ${attributes.length}`);
        console.info(`Has user input | Items: ${Object.keys(obj[x])}`);
        let businessConditionStatus = Object.keys(obj[x]).businessConditionStatus;
        pm.expect(businessConditionStatus.value.to.eql(false));
        
    })
});

```

---

<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:** [September 2, 2023, 5:54pm UTC](https://community.postman.com/t/how-to-do-a-assertion-on-a-nested-json-array/51261/5 "2023-09-02T17:54:17Z")

</div>

Really not sure what you are trying to do with that code.

However, here are two options as I suggested in my earlier post.

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

let array = response.entities[0].data.attributes.businessConditions.group;
// console.log(array);

// option 1 - loop through the array
array.forEach(obj => {
    businessConditionName = obj.businessConditionName.values[0].value
    // console.log(businessConditionName);
    businessConditionStatus = obj.businessConditionStatus.values[0].value
    // console.log(businessConditionStatus);
    pm.test(`${businessConditionName} status is not false`, () => {
        pm.expect(businessConditionStatus).to.not.be.false;
    })
});

// option 2 - retrieve the statuses and just check that they don't include false
let businessConditionStatuses = array.map(({ businessConditionStatus }) => businessConditionStatus.values[0].value);
// console.log(businessConditionStatuses);
pm.test("businessConditionStatuses statuses do not include false", () => {
    pm.expect(businessConditionStatuses).to.not.include(false);
}) 

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/6/96e4c51796478d456a45b72e13c032c1ef929700.png)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [September 5, 2023, 5:55pm UTC](https://community.postman.com/t/how-to-do-a-assertion-on-a-nested-json-array/51261/6 "2023-09-05T17:55:05Z")

</div>

Closed on September 5, 2023, 5:55pm UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
