How to do a assertion on a nested JSON array?

Hi,

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

{
    "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
                                            }
                                        ]
                                    }
                                }                              
                            ]
                        }
                    }
                }
            }
        ]
    }
}

Hey @omernauman :wave:

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.

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).

1 Like

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

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));
        
    })
});

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

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

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);
}) 

2 Likes

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