0

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?

DastanAli
  • 15
  • 7
  • 1
    This test seems wrong - you parse the response body, go over it's keys and then check that each key is present in the response. By definition, this will always be true, because you got the list of keys from the response itself. Did you perhaps mean to check this against a predefined list of keys? – Mureinik Sep 18 '20 at 17:17

1 Answers1

0

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");
}
Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
  • Thanks, it worked for me, in the end I did it like this: `var data = JSON.parse(responseBody); let hasProperties = true; Object.keys(data).forEach(key => { if!(data.hasOwnProperty(key)){ hasProperties = false; } }); if (hasProperties) { tests["Have all properties"] = hasProperties === true; }` – DastanAli Sep 18 '20 at 17:31