Search code examples
jsonobjectpostmanpostman-testcase

Postman Object hasOwnProperty Tests


I am trying to evaluate a JSON, so that I can know if the properties are correct, I have the following code:

var data = JSON.parse(responseBody);

Object.keys(data).forEach(key => {
 if(data.hasOwnProperty(key)){
   console.log("Have all properties");
 }
});

The problem I have is that, the answer is shown to me "n" times, how can I make it show it to me only once after evaluating that the properties exist?


Solution

  • This should do it:

    var data = JSON.parse(responseBody);
    
    let hasProperties = true;
    
    Object.keys(data).forEach(key => {
     if!(data.hasOwnProperty(key)){
       hasProperties = false;
     }
    });
    
    if (hasProperties) {
       console.log("Have all properties");
    }