Look if a property exists in responseBody

Hi guys,

Iā€™m trying to do something that seems to be a little more complicated than I thought.

Hereā€™s the thing. For a specific request, sometimes my response body will contain :
"playerReference":"+33123456789"
and sometimes, not. Iā€™m not saying that sometimes ā€œplayerReferenceā€ is null, but it is simply NOT in the response body. And I want to create a condition on wether or not ā€œplayerReferenceā€ exists and I canā€™t :frowning:

I tried something like that

if(pm.expect(jsonData.playerReference).to.be.an('undefined') === true) {
    console.log("there is no player reference");
}
else
{
    console.log("there is a player reference");
} 

Or even

if(pm.expect(pm.response.text()).to.include('playerReference') === true) {
    console.log("there is player reference");
}
else
{
    console.log("there is no player reference");
}

I really donā€™t understand why the last one doesnā€™t work.

Postman documentation is usually really helpful on this topic. However, Iā€™m struggling to find a proper solution to my problem, here.

Any ideas ?

[RESOLVED]

I changed my condition to simple javascript and now itā€™s working

if(pm.response.text().includes('playerReference') === true) {
    console.log("there is a player reference");
}
else
{
    console.log("there is no player reference");
}

Iā€™m about to leave work, so Iā€™ve only skim-read your post - sorry if this isnā€™t what you want.

But you could use the ā€œhasOwnPropertyā€ method.

if(pm.response.json().hasOwnProperty("playerReference") {
   console.log("The property is there!");
} else {
  console.log("The property isn't there :(");
}

I always test for properties/values like this:

const body = pm.response.json();
// The below will fail and give us a helpful failure message if "playerReference" is NOT present
// And it'll also give us a helpful failure message if the value of "playerReference" is NOT "123"
pm.test("Property is there", function() {
  pm.expect(body).to.have.property("playerReference", "123");
});

Right! Home time for me :slight_smile:

3 Likes

I didnā€™t know about hasOwnProperty
I also have to head home so Iā€™ll test it tomrrow :slight_smile:
Thank you sharing it !

hey @steven_kp Did the code snippet work for you?

Hello pfarrel,

In my case, hasOwnProperty doesnā€™t seem to work. It returns me false even though the property is thereā€¦

Anyway, I found a workaround using pm.response.text() and .includes()

Thank you :slight_smile:

EDIT:
Actually it is working. I did a test with a property that was inside another property, hence the error.
ā€œloginInformationā€: {
ā€œsessionā€: ā€œ99456489ā€ }
If I do hasOwnProperty(ā€œloginInformationā€), Iā€™ve got true. However, if I do hasOwnProperty(ā€œsessionā€), Iā€™ve got false

Yep, thatā€™s because the ā€œsessionā€ property, is inside the ā€œloginInformationā€ object.

So if you did:

const body = pm.response.json();
body.hasOwnProperty("loginInformation");
// This will output true
body.hasOwnProperty("session");
// This will output false as we're only looking at the top-level of the response, any nested objects won't return true.
body.loginInformation.hasOwnProperty("session");
// This will now output true
1 Like