Verify all values of returned id match in nested array

Hello.
I am struggling with nested arrays as response.
Here an example of a response I get:

[
    {
        "id": "ididididid",
        "firstName": "user1",
        "lastName": "user1",
        "email": "user1@domain.com",
        "replyToAddress": "user1@domain.com",
        "phoneNumber": "1234567890",
        "internalUser": false,
        "status": "Active",
        "userStatus": {
            "id": "user_status_id",
            "name": "Active",
            "enabled": true
        },
        "groups": [
            {
                "branch": {
                    "id": "branch_1_id"
                },
                "role": {
                    "id": "role_1_id"
                }
            }
        ],
        "company": {
            "id": "company_1_id",
            "roles": [
                {
                    "id": "company_role_1_id"
                }
            ]
        }
    }
]

What I have tried so far is:

pm.test("Company is example_company_name", () => {
        _.each(responseBody.company, (item) => {
            pm.expect(item.company.id).to.equal("company_1_id")
    })
});

But it does not seem to work correctly.

Response body contains hundreds of elements like that.

What I am trying to do is:

  • Want to compare all returned company_id’s are the same and equal to a specific value (let’s say company_1_id).

Please, also advice on a proper source to get familiar with this structure and syntax on how to find nested elements.

Thanks all!

The main problem is logic in your assertion that you were trying to iterate over the **responseBody.company object,** which is not an array and doesn’t have the .each function. You should be iterating over the main responseBody array instead. Additionally, in your assertion, you were trying to access item.company.id , but in the response structure, company.id is directly under the user object, not under a nested company property.r
correct assertion is

pm.test("All Company IDs are company_1_id", () => {
    _.each(responseBody, (user) => {
        pm.expect(user.company.id).to.equal("company_1_id");
    });
});
try it...
1 Like

Hi,

Thank you!
But how is (user) accepted in this case?
It is never defined as a variable or an object in the main array?

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