Verify content in the response

Question:
I am trying to verify the first name is equal what is expected.

I have tried:
pm.test(“Person is CHAXXXXX”, () => {

const responseJson = pm.response.json();

pm.expect(responseJson.validAccounts[0].firstName).to.eql(‘CHAXXXXX’);

});

Result - Person is CHAXXXXX | TypeError: Cannot read property ‘0’ of undefined

This is the response body

{
“sytemResponse”: {
“responseCode”: 200,
“responseDescription”: “OK”
},
“response”: {
“validAccounts”: [
{
“firstName”: “CHAXXXXX”,
“accountNumber”: “123”
},
{
“firstName”: “CHAXXXXX”,
“accountNumber”: “456”
}
],
“validationResult”: {
“isValid”: true,
“resultCount”: 2,
“validationResultDescription”: “Success”
}
}
}

Hi @automationtester2496

On first glance I think your assertion should be;

pm.expect(responseJson.response.validAccounts[0].firstName).to.eql(‘CHAXXXXX’);

responseJson is holding the entire response, but there is a ‘response’ object inside the JSON response (hope that makes sense).

Thank you very much that is the solution.

1 Like

@w4dd325 is there anyway to loop through the objects in the response body, to solve the following issue?

Replied on the thread ^

1 Like