How assert object inside array

Hi,
i’m trying to assert if a certain object is of a certain type.

[
    {
        "id": 25,
        "username": "username",
        "firstname": "First",
        "lastname": "Last",
        "personalIdentityNumber": null,
        "roles": {
            "client": {
                "clientNumber": null,
                "employments": [
                    "111",
                    "555"
                ]
            },
            "mainAdministrator": {}
        },
        "active": true,
        "email": null,
        "careOf": null,
        "street": null,
        "zipCode": null,
        "city": null,
        "phoneNumber": null,
        "cellPhoneNumber": null
    },
    {...},
    {...},
    {...},
    {...}
]

Right now i’m trying to find out if the user has the role of client.
How can I assert that?

Hi @joachim.sjogren,

Welcome to the community! :clap:

From what I see, this should be fairly simple to do in your Tests script section.

You’d first want to drill down to the “roles” attribute, and check to see if it contains a “client” object.

pm.test("Assert Client Role", function () {

    var users = pm.response.json();
    var check = (users[0] && users[0].roles && typeof users[0].roles.client !== 'undefined') ? true : false
    pm.expect(check).to.eql(true)

});

Now this will only check for the first user, if you want to check for all users, you can loop over your responses, or you can do map as well, both should achieve the desired result if you want to check all users.

Hope this helps!

2 Likes