Validating Json array using java script

Hi guys need help in validating the json response. I need to validate the json array values and if atleast one value > specific condition my script should pass. below is the json

[
  {
    "batch_id": xxxxxxx,
    "last_revision_id": xxxxxxx,
    "priority": "xxxxxxx",
    "name": "xxxxxxxx",
    "items": 14,
    "print_by": "xxxxxxx",
    "created": "2xxxxxxx0",
    "jsactions": [
      {
        "label": "xxxxxxx",
        "action": "xxxxxxx",
        "icon": "xxxxxxxx"
      },
      {
        "label": "xxxxxxx",
        "action": "xxxxxx-xxxxx",
        "icon": "xxxxxxxx",
		}
    ],
    "disappear_date": fgfgh,
    "filtered_by_default_tags": fhfghg,
    "assignments": 0,
    "undeletable_assignments": gggfh
  },
  {
    "batch_id": xxxxxxx,
    "last_revision_id": xxxxxxx,
    "priority": "xxxxxxx",
    "name": "xxxxxxxx",
    "items": 5456,
    "print_by": "xxxxxxx",
    "created": "2xxxxxxx0",
    "jsactions": [
      {
        "label": "xxxxxxx",
        "action": "xxxxxxx",
        "icon": "xxxxxxxx"
      },
      {
        "label": "xxxxxxx",
        "action": "xxxxxx-xxxxx",
        "icon": "xxxxxxxx",
		}
    ],
    "disappear_date": fgfgh,
    "filtered_by_default_tags": fhfghg,
    "assignments": 0,
    "undeletable_assignments": gggfh
  }
    
]

I used normal if condition using javascript but it is not validating properly

var jsonData = JSON.parse(responseBody);
if (responseBody.has("items")) {
   if(jsonData[0].items >=300){
   tests["Is atleast one of the batch in future >=300"] = responseBody.has("items");
}
}
else {
    tests["The batch is empty"] = responseBody.has("njmkhkhull");
}

But the test getting passed irrespective of the values present in the json array. Even if the json value is 10 if i give the condition as >=30000 it is showing the test as passed.

I used the _.each(pm.response.json(), (arrItem) => function it is working fine but this method is not suited for my result analysis as it validates all the array elements.

My desired condition is even if one among the array values satisfies the condition test case should be passed. so that only one value will be logged in console either in case of pass or fail. In case of .each it is logging for all the array values and its cumbersome

please help on this

Hey @aravaws,

Are you able to provide an example of the structure of your response, please?

You can just paste an example of the JSON here rather than an image. Mask or add dummy values for any sensitive information.

This will help users to run things locally and help provide a solution quicker. :smiley:

[
  {
    "batch_id": xxxxxxx,
    "last_revision_id": xxxxxxx,
    "priority": "xxxxxxx",
    "name": "xxxxxxxx",
    "items": 14,
    "print_by": "xxxxxxx",
    "created": "2xxxxxxx0",
    "jsactions": [
      {
        "label": "xxxxxxx",
        "action": "xxxxxxx",
        "icon": "xxxxxxxx"
      },
      {
        "label": "xxxxxxx",
        "action": "xxxxxx-xxxxx",
        "icon": "xxxxxxxx",
		}
    ],
    "disappear_date": fgfgh,
    "filtered_by_default_tags": fhfghg,
    "assignments": 0,
    "undeletable_assignments": gggfh
  },
  {
    "batch_id": xxxxxxx,
    "last_revision_id": xxxxxxx,
    "priority": "xxxxxxx",
    "name": "xxxxxxxx",
    "items": 5456,
    "print_by": "xxxxxxx",
    "created": "2xxxxxxx0",
    "jsactions": [
      {
        "label": "xxxxxxx",
        "action": "xxxxxxx",
        "icon": "xxxxxxxx"
      },
      {
        "label": "xxxxxxx",
        "action": "xxxxxx-xxxxx",
        "icon": "xxxxxxxx",
		}
    ],
    "disappear_date": fgfgh,
    "filtered_by_default_tags": fhfghg,
    "assignments": 0,
    "undeletable_assignments": gggfh
  }
    
]

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 will give you an idea of other assertions that could be made in the pm.expect() statements:

https://www.chaijs.com/api/bdd/

Thanks a lot I will try and let you know