How to check if an object exist in POSTMAN?

Mehdy Driouech picture Mehdy Driouech · May 15, 2017 · Viewed 33.3k times · Source

I want to know how can i test if an object exist. For example my API return these things :

"data": [
    {
      "id": 1,
      "name": "Abu Dhabi",
      "locale": "AE",
      "rentWayCountryId": 242,
      "stations": [
        {
          "id": 2,
          "rentWayName": "ABU DHABI AIRPORT",
          "rentWayStationId": "IAEAUH1",
          "bindExtrasToStationToExtraCategory": []
        }
      ]
    },

I want to check that data.id exist.

I used the test options in postman and i did this :

var jsonData = JSON.parse(responseBody);
tests["Name value OK"] = jsonData.data.id === "1";

Could you tell me witch condition should i use to verify only if the data exist.

Thanks you very much !!

Answer

EricWasTaken picture EricWasTaken · Jan 13, 2018

Here is a proper Postman test:

const jsonData = pm.response.json();

pm.test('Has data', function() {
  pm.expect(jsonData).to.have.property('data');
});

The above will either PASS or FAIL yor postman request based on the presence of the data property in the response.