How to do a chained assertion on a nested JSON array which is a part of JSON Response

I’m looking for a chained postman chai bdd assertion where test should PASS if the response body contains specific key value pairs which happens to be within a nested JSON Array. If not possible to have single chained assertion, happy to split it into multiple assertions for each reason code as per their existence requirement.

For Example: Say below is the JSON response body

{
“item”: {
“code”: “A1234Z”,
“name”: “Sample Item Name”,
“status”: “REJECTED”
},
“price”: {
“baseAmount”: “10.0”,
“pricingType”: “RRP”
},
“reasonCodes”: [
{
“code”: “123”,
“text”: “This is a REJECT reason code 123.”,
“type”: “R”
},
{
“code”: “456”,
“text”: “This is a WARN reason code 456”,
“type”: “W”
},
{
“code”: “789”,
“text”: “This is a INFO reason code 789”,
“type”: “I”
}
]
}

Criteria: Test should pass if the reasonCodes Array contains codes 456 & 789, but not 123.

Any help is much appreciated. I’ve attached a sample trial I’ve done using include.deep.members, where I’m able to get what I wanted if the array contains only code elements in it without any other elements. See the screenshot attached.
image|690x399

Do you have a set of reason codes you specifically do not want to allow? That would probably be the easiest thing to do. I might structure it like this:

const invalidReasonCodes = ['123'];

pm.test("Does not have any invalid codes", function () {
    const jsonData = pm.response.json();
    const invalidCodes = jsonData.data.reasonCodes.filter(rc => invalidReasonCodes.indexOf(rc.code) > -1);
   
    pm.expect(invalidCodes.length).to.eql(0);
});

Invalid codes can be 1 or more.

Thanks for the reply, but my test pass criteria is that the reasonCodes Array must contain codes 456 & 789, but not code123. Also the invalid codes can be single or multiple at times.

To simplify my query, if response json is an array as below then test must PASS if codes 1 & 2 are found and 4 & 5 are not found in it.

var rsp = [{“code”: “1”, “text”: “one”}, {“code”: “2”, “text”: “two”}, {“code”:“3”, “text”: “three”}];

Could you please suggest a solution for it?

I was offering something a little more generic, but if you want something that is explicitly what you said, sure.

const jsonData = pm.response.json();
const has456 = (jsonData.data.reasonCodes.indexOf(rc => rc.code === '456') > -1);
const has789 = jsonData.data.reasonCodes.indexOf(rc => rc.code === '789') > -1);
const doesNotHave123 = (jsonData.data.reasonCodes.indexOf(rc => rc.code === '123') == -1);

pm.expect(has456).to.be.true;
pm.expect(has789).to.be.true;
pm.expect(doesNotHave123).to.be.true;

Can you please confirm if this was what you mean for my simplified response json, for which I didn’t get the test PASS:

pm.test(“Response must have string code & values 1, 2 but not 4, 5 (ignoring 3)”, function(){

var rsp = [{"code": "1", "text": "one"}, {"code": "2", "text": "two"}, {"code":"3", "text": "three"}];
console.log(rsp);

const has1 = (rsp.indexOf(rc => rc.code === '1') > -1);
const has2 = (rsp.indexOf(rc => rc.code === '2') > -1);
const not4 = (rsp.indexOf(rc => rc.code === '4') == -1);
const not5 = (rsp.indexOf(rc => rc.code === '5') == -1);

pm.expect(has1).to.be.true;
pm.expect(has2).to.be.true;
pm.expect(not4).to.be.true;
pm.expect(not5).to.be.true;

});

Attached is the test result:

I’ve tried what you suggested, but didn’t get expected outcome for that test.

You can make it simpler by using find instead of indexOf.

var rsp = [{"code": "1", "text": "one"}, {"code": "2", "text": "two"}, {"code":"3", "text": "three"}];
console.log(rsp);

const has1 = rsp.find(rc => rc.code == '1');
const has2 = rsp.find(rc => rc.code == '2');
const not4 = rsp.find(rc => rc.code == '4');
const not5 = rsp.find(rc => rc.code == '5');

pm.expect(has1).to.not.be.undefined;
pm.expect(has2).to.not.be.undefined;
pm.expect(not4).to.be.undefined;
pm.expect(not5).to.be.undefined;

The above worked for me when i just tried it.

1 Like

Yeah! Thanks for your help Allen. Just wanted to know if there is any way to do a chained assertion to acheive the same outcome.