How can I search the entire response for a specific value?

Hello. Sorry for the noob question.

How can I search the entire response for a specific value? In this case the value I am using as my assertion would be let’s say “222222”, this has been set as an environment variable (“provId”) in another request and I need to be able to assert that it appears in this second request.

Below is a small snippet of the response I’m working with, the acual response would be much larger and the value “222222” could appear anywhere within.

[
{
“eventId”: “123456”,
“mappings”: [
{
“provider”: “ABC”,
“providerId”: “111111”,
“confidence”: 1.0
}
]
},
{
“eventId”: “246810”,
“mappings”: [
{
“provider”: “ABC”,
“providerId”: “222222”,
“confidence”: 1.0
}
]
},
{
“eventId”: “135791”,
“mappings”: [
{
“provider”: “ABC”,
“providerId”: “333333”,
“confidence”: 1.0
}
]
}
]

I have managed to write a bit of code to check the correct value from the 1st object (below) but I need it to search the whole response.

const body = pm.response.json();
pm.test(“placeholder” + pm.variables.get(“provId”), function() {
pm.response.to.have.jsonBody("[0][mappings].[0][providerId]", pm.variables.get(“provId”) );
});

1 Like

Hi @Leon Welcome to the Community :slight_smile:

Tried to add the response via postman echo:

{
    "args": {},
    "data": [
        {
            "eventId": "123456",
            "mappings": [
                {
                    "provider": "ABC",
                    "providerId": "111111",
                    "confidence": 1
                }
            ]
        },
        {
            "eventId": "246810",
            "mappings": [
                {
                    "provider": "ABC",
                    "providerId": "222222",
                    "confidence": 1
                }
            ]
        },
        {
            "eventId": "135791",
            "mappings": [
                {
                    "provider": "ABC",
                    "providerId": "333333",
                    "confidence": 1
                }
            ]
        }
    ],
    "files": {},
    "form": {},
    "headers": {
        "x-forwarded-proto": "https",
        "x-forwarded-port": "443",
        "host": "postman-echo.com",
        "x-amzn-trace-id": "Root=1-60d9ae0f-490add847b36b287777bd907",
        "content-length": "390",
        "content-type": "application/json",
        "user-agent": "PostmanRuntime/7.28.1",
        "accept": "*/*",
        "cache-control": "no-cache",
        "postman-token": "35370ccc-0ebb-4e84-938b-9c65b4ff563a",
        "accept-encoding": "gzip, deflate, br",
        "cookie": "sails.sid=s%3AGagXy-GC8a3ZnoOXWxQjcboSzysfmNLH.VYgXzch5Kjdnxk0COTbsRl82CXQ%2BbRhWqML83v%2F7PCw"
    },
    "json": [
        {
            "eventId": "123456",
            "mappings": [
                {
                    "provider": "ABC",
                    "providerId": "111111",
                    "confidence": 1
                }
            ]
        },
        {
            "eventId": "246810",
            "mappings": [
                {
                    "provider": "ABC",
                    "providerId": "222222",
                    "confidence": 1
                }
            ]
        },
        {
            "eventId": "135791",
            "mappings": [
                {
                    "provider": "ABC",
                    "providerId": "333333",
                    "confidence": 1
                }
            ]
        }
    ],
    "url": "https://postman-echo.com/post"
}

Here is the assertion to find the property id:

var resp = pm.response.json();

console.log(resp);

 for (var i = 0;i<resp.data.length;i++) 

{

     value = pm.environment.get("value_check"); //env variable that has the property id

     //console.log(value);

     if (resp.data[i].mappings[0].providerId == value)

     {

         console.log("Property ID found for : " +resp.data[i].eventId);

     }

     else 

     {

         console.log("Property ID not found for : " +resp.data[i].eventId);

     }

}
1 Like
let a =
    [
        {
            "eventId": "123456",
            "mappings": [{
                "provider": "ABC",
                "providerId": "111111",
                "confidence": 1.0
            }
            ]
        },
        {
            "eventId": "246810",
            "mappings": [{
                "provider": "ABC",
                "providerId": "222222",
                "confidence": 1.0
            }
            ]
        },
        {
            "eventId": "135791",
            "mappings": [{
                "provider": "ABC",
                "providerId": "333333",
                "confidence": 1.0
            }
            ]
        }
    ]


console.log(a.find(elem => elem.mappings[0].providerId === "333333"))

use array.find

Thank you very much. I ended up adapting it slightly to the below (mainly as the response I am working with is inside an unnamed array)

var resp = pm.response.json();
console.log(resp);
for (var i = 0;i<resp.length;i++)
{
if (resp[i].mappings[0].providerId == pm.variables.get(“provId”))
{
console.log("PASSED TEST : " +resp[i].eventId);
}
else
{
console.log("FAILED TEST : " +resp[i].eventId);
}
}

1 Like

Hi @Leon.

Glad it helped :slight_smile: