Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

postman read property in json response which name is saved in array

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading