Home > Back-end >  postman read property in json response which name is saved in array
postman read property in json response which name is saved in array

Time:11-05

I am trying to check values of certain properties in json response in postman.

pm.expect(jsonData.property).not.equal(null);

The thing is, that I need to check multiple properties which names I have stored in an array.

var jsonData = pm.response.json();
var keys = ["id", "phoneNumber", "passwordHash"];

pm.test("All values have valid value", function(){
   for(var a = 0; a < keys.length; a  ){
      pm.expect(jsonData.keys[a]).not.equal(null);
   };
});

But I am getting this error:

All values have valid type | TypeError: Cannot read property '0' of undefined

Can someone explain me, what am I doing wrong please? Thanks for any advice.

CodePudding user response:

You cann't use (.) dot to get a value of a unknown key, use [] bracket instead. Try this:

pm.expect(jsonData[keys[a]]).not.equal(null);

One subtle thing, in my opinion, use i (mean index) is better than a.

  • Related