My question :
Hi! if i have a json like this: {“a”:1,“b”:2}. i want to prove that any other property does not exist. like, “c” not exist but at the same time, “name” do not exist.
I don’t want to do pm.expect(jsonData).not.to.include([“c”,“d”,“name”,“number”,etc])
bbahadur
(Bharat Bahadur)
February 21, 2022, 4:20pm
2
Hey @spacecraft-astronau2 , Welcome to the community
I think what you are looking for, is to validate the property. You can use ‘.hasOwnProperty’ method to check for it:
//example
pm.test("Properties validated",()=>{
pm.expect(response.hasOwnProperty('a')).to.be.true;
pm.expect(response.hasOwnProperty('b')).to.be.true;
pm.expect(response.hasOwnProperty('c')).to.be.false;
// .....etc
})
If your response includes an array of data, have a look at this scenario:
Hey @hasmukhpatel !
If you want to check if ‘data’ has all properties, you can iterate through each and perform assertion. Here is one way to achieve it:
let jsonData = pm.response.json();
console.log(jsonData.data);
pm.test("All keys present",()=>{
_.each(jsonData.data, (item)=>{
pm.expect(item.hasOwnProperty('start_date')).to.be.true;
pm.expect(item.hasOwnProperty('score')).to.be.true;
pm.expect(item.hasOwnProperty('unit')).to.be.true;
pm.expect(item.hasOwnProperty('st…
Good luck
Hope it helps.