Help on checking items in response

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Hello everyone,

Is there any way for postman to check if a specified field contains one or more items below. For a sample response below:

{
    "id": "4090ecfa537e83f33ebaf4bb368e2b0f",
    "r": {
        "UsedBy": [
            {
                "id": "f44fb93d09c188624ef2d53c89a4237e",
                "type": "Parameter",
                "name": "ASDF123"
            },
            {
                "id": "ce3a770f4d4d9057fa8ad11f269ada87",
                "type": "Parameter",
                "name": "QWERT123"
            }
        ]
    }
}

Say, if there are any items below r: (UsedBy[0] and [1] in this case), the test passes.

But an empty list such as below fails the test:

{
    "id": "4090ecfa537e83f33ebaf4bb368e2b0f",
    "r": {}
}

Hello @mpnistal19 , so you are telling that just the property “r” presence should pass the Test cases?

In that case, please check that the below snippet works for you :slightly_smiling_face:

var resp = JSON.parse(responseBody);

 pm.test("'r' property availbility check", function()
{
  pm.expect(resp.r).to.exist);
});

If this doesn’t answer your question, kindly share your Test and explain what are you trying to Test.

Thanks @bpricilla will try it. :slight_smile: Basically, if there’s anything inside “r”: {} it’ll pass the test.

It’s actually one of my first steps, soon I will have to check for specific numbers of items under r but for one I willjust have to verify if there’s anything under.

1 Like

Hello
This test will check if given key contains R key and in next it will test if it contain at least on of the given keys

if(pm.response.to.be.success){
    pm.test("respons has R key", function() {
      pm.expect(pm.response.json()).to.have.property('r');
    });
    
    pm.test("R key is an object", function() {
      pm.expect(pm.response.json().r).to.be.an('object').that.has.any.keys('UsedBy');
    });
}

You can change .any.keys to .all.keys if you expect only this one key or list of them to appear.
More you can find in official docs

1 Like

Thank you very much, pal. I got a lot of help from the resources you shared.