Find item in array by specific properties

Hi folks, I am vey new to postman. I have an array as response and I want to check if a specific item is present. I can find the item by its properties. I see in all the similar topics it is possible to use find() but I cannot make it work. May you please help me?
here it is an example of the response bdy:

        "districtName": "Austria",
        "storeName": "Store6",
        "salePerson": "sdfgh",
        "isPrimary": false
        "districtName": "Austria",
        "storeName": "Store6",
        "salePerson": "dfsd",
        "isPrimary": false
        "districtName": "Austria",
        "storeName": "Store6",
        "salePerson": "Chuck Norris",
        "isPrimary": true

Let’s say I want to verify the item with saleperson===“Chuck Norris is present” and it has IsPrimary set to true . How can I do?

Hey @ignleotta :wave:

Welcome to the Postman community! :postman:

These type of questions are quite common and fall more on the JavaScript side than being a specific Postman problem.

It’s always good to do a search across the forum first to check for similar questions.

Here’s on response I gave a while back the you can use here:

You would need to change the references to your context but it will work the same. Give it a go first and then come back if you have any issues.

@danny-dainton I read mean posts already and used a solution similar to what you proposed but unfortunately i also returns:
TypeError: Cannot read properties of undefined (reading ‘find’)

Without seeing what you have in the script, I’d going to be difficult to solve that.

It’s probably just a reference issue as that other question would have had a different response structure. You would need to edit that references to match your structure.

At the end I found a solution without using find or filter:

pm.test('ResposehasAPrimary', function() {
_.each(pm.response.json(), (item) => {
    if(item.salePerson === 'Chuck Norris') {
     pm.expect(item.isPrimary=== true);

Thanks @danny-dainton for his answer to another post which I used to find this solution.

Wouldn’t you want your assertion to be something like:


I’m not sure that yours would be working as expected.

Here are two ways to test for those conditions using the JavaScript find function.

Option 1 searches on both conditions, and for the test you just need to check that the search is not undefined.

Option 2 searches for the name, and the test then checks if the isPrimary key = true in the returned object.

const response = pm.response.json();


pm.test('Sales person = Chuck Norris and isPrimary = true V1', () => {
    let searchV1 = response.find(obj => obj.salePerson === "Chuck Norris" && obj.isPrimary === true);

pm.test('Sales person = Chuck Norris and isPrimary = true V2', () => {
    let searchV2 = response.find(obj => obj.salePerson === "Chuck Norris");

Good practice is to make your test fail to ensure that you are not getting a false positive. For example, change the “” to “” and ensure it fails the test. You should do this with all assertions. Make it pass, then make it fail, before setting it back to pass. This way, you know the assertion is working as expected.


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.