Loop through response array multiple objects to find if value of each object is within(or equal) to parameters given in request

My question:
I have request with parameters (image 1)
I 'am getting response with array with multiple objects . I need test to go through and check if each object have value between or equal to values in request.
My final goal is to create pm.test where I can check that all of objects are inside those values
response - images -2,3
Thank you

Details (like screenshots):
1 - image
2 -


3 -

How I found the problem:

I’ve already tried:

hey @sergeyor :wave: Welcome to the community :partying_face:

So the request which gives you the parameters, ‘orgUnitFrom’ and ‘orgUnitTo’ you can either save these value to the Collection or Environment variables to use them later in another request or you can just copy these value and use in your Test function.

In the request where you have the array of objects, just run a loop and check for ‘orgUnit’ to be between the parameter, one way you can do this is:

let response=pm.response.json()

let orgUnitFrom= 390101020  

let orgUnitTo= 390102101

//you can also store those value in Environment or Collection variables in 
//first request and retrieve them here
//pm.collectionVariables.get("orgUnitFrom") 
// pm.environment.get("orgUnitFrom")

pm.test("Unit check",()=>{
    _.each(response.results, (item)=>{
       // console.log(orgUnitFrom,item.orgUnit,orgUnitTo)
        pm.expect(orgUnitFrom<=item.orgUnit?true:false).eqls(true)
        pm.expect(orgUnitTo>=item.orgUnit?true:false).eqls(true)
    })
})

Hope this helps,
Good luck!

Hi @bbahadur
Thank you for reply
If I understand correctly the pm.test only check if response values are equal to request values
but response have also values between those two values.
Can your test also check for those between values?

The lines

 pm.expect(orgUnitFrom<=item.orgUnit?true:false).eqls(true)
 pm.expect(orgUnitTo>=item.orgUnit?true:false).eqls(true)

will check each response values to be between those values, also also if they are equal.

It’s working
Thank you very much

1 Like