Validate json values and pass the test if at least one of the Json values matches the specified condition

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Hi Guys,

I am facing a situation, where in I need to capture all the values from a particular Json key and need to pass the test if at least one among the captured values matches the test condition specified by me.

Actually I have a not so good solution already , where in I compare all the json values using something like this

if (responseBody.has("items")) {
    tests["Is the batch present >=20"] = jsonData[0].items || jsonData[1].items || jsonData[2].items >=20;
}
else {
    tests["The batch is empty"] = responseBody.has("null");
}

But the problem with the above one is that I don't know total number of items keys that will be present in the response.


I tried to dynamically capture all the json values using the below code

if (responseBody.has("items")) {
    _.each(pm.response.json(), (arrItem) => {

        // To print the array item in the console log
        //console.log(arrItem.items)

        // To check all the items in array 
        //pm.expect(arrItem.items).to.have.above(5)

        // To find the total count of the array values
        /* if(arrItem.items) {
                 count++;
             }
          //pm.environment.set("count", count); */

        // To validate all the elements in array , else is wantedly left blank   

        if (arrItem.items >= 300) {
            tests["The batch has atleast one items >= 300"] = responseBody.has("items");
        } else {
           tests["The batch has values < 300"] = responseBody.has("fdgfd");
        }
    })
}

else {
    tests["The batch is empty"] = responseBody.has("null");
    //console.log(Store_number);
}

Everything is fine with the above code but since its looping through all the individual values I don’t know how to make the script pass when atleast one of the values in arrItem.items >= 20

Thanks in Advance

Finally I solved the issue. Please check the solution on this link