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

**URL:** <https://community.postman.com/t/validate-json-values-and-pass-the-test-if-at-least-one-of-the-json-values-matches-the-specified-condition/12613>\
**Category:** Help Hub\
**Tags:** json\
**Created:** [May 13, 2020, 1:52pm UTC](https://community.postman.com/t/validate-json-values-and-pass-the-test-if-at-least-one-of-the-json-values-matches-the-specified-condition/12613 "2020-05-13T13:52:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aravaws](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@aravaws](https://community.postman.com/u/aravaws)\
**Post date:** [May 13, 2020, 1:52pm UTC](https://community.postman.com/t/validate-json-values-and-pass-the-test-if-at-least-one-of-the-json-values-matches-the-specified-condition/12613/1 "2020-05-13T13:52:01Z")

</div>

**Your question may already have an answer on the community forum. Please search for related topics, and then read through the [guidelines](https://community.postman.com/t/about-the-help-category/38) 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

```auto
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

---

<div class="post-metadata">

**Author:** ![aravaws](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@aravaws](https://community.postman.com/u/aravaws)\
**Post date:** [July 7, 2020, 7:22am UTC](https://community.postman.com/t/validate-json-values-and-pass-the-test-if-at-least-one-of-the-json-values-matches-the-specified-condition/12613/2 "2020-07-07T07:22:59Z")

</div>

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

> [@Validating Json array using java script](https://community.postman.com/t/validating-json-array-using-java-script/13721/4):
>
> You could use something like this to loop through and filter the objects that contain items greater than 300. It will create an array that you can assert against the length of to see if it’s above 1. I’ve added in a custom error string that shows you why it failed. pm.test('Is atleast one of the batch in future \>=300', () =\> { let result = pm.response.json().filter(arr =\> arr.items \>= 300) pm.expect(result.length).to.be.above(1, `Objects matching condition ${result.length}`) }) This w…
