How to test fields in array

Hello guys. I receive a JSON from api and it need to test by Postman

{
    "d": [
        {
            "id": 1,
            "name": "John",
            "title": "Doe",
            "service": "Postman",
            "tr": [
                {
                    "id": 28,
                    "tId": 237,
                    "la": "p5",
                    "su": "z0",
                    "bo": "y3",
                    "au": {
                        "CAT": "2022-07-08 07:18:28",
                        "CABY": 1254,
                        "UAT": "2022-07-08 07:18:28",
                        "UBY": 1254
                    }
                }
            ],
          }
}

My question is, How to test fields in “d” array and “tr” array.
I need to know, that I have, for example “title” field and “title” field has “Doe”.
And “tld” field is exit and has 237.
And CABY field is exit and has 1254.

Please help me with arrays

Hi @Ivaneo47

You would use something like this.

const response = pm.response.json();

pm.test("My Tests", () => {
    pm.expect(response.d[0].title).to.eql("Doe");
    pm.expect(response.d[0].tr[0].tId).to.eql(237);
    pm.expect(response.d[0].tr[0].au.CABY).to.eql(1254);
});

Note; to access arrays you have to identify the index, in this example, it is [0].

1 Like