Checking JSON respond with test script

Hello there,

I’m trying to check a value from the JSON respond I get.

The JSON Body I get is:

{
    "HolidayRightsResults": [
        {
            "NiszNumber": "78121925128",
            "HolidayRights": [
                {
                    "AFWZ_TYP": "01",
                    "BGN_DAT": "2019-01-01T00:00:00",
                    "BGN_SLD_OPN_DG": 20.00,
                    "BGN_SLD_OPN_URN": 152.00,
                    "BRK_RCHT_DG": 20.00,
                    "BRK_RCHT_URN": 152.00,
                    "DSSR_NR": "1HI0269",
                    "DateIn": "2014-08-01T00:00:00",
                    "DateOut": null,
                    "END_DAT": "2020-01-01T00:00:00",
                    "OPGN_DG": 4.00,
                    "OPGN_URN": 30.40,
                    "Hrk_cod": "S",
                    "WNNR": "0001876",
                    "OPGB_RCHT_URN": 152.00,
                    "OPGB_RCHT_DGN": 20.00,
                    "RCHT_DG_WZG_DT": "2019-08-21T07:51:21.296431",
                    "RCHT_URN_WZG_DT": "2019-08-21T07:51:21.296431",
                    "RCHT_EXPRT_DT": "2019-08-21T07:51:37.309306"
                }
            ],
            "CalculationMessages": []
        }
    ]
}

What I’m trying to do is to check the NiszNumber.
My test script is:

var response = JSON.parse(responseBody);
tests["Nisznumber is 78121925128"] = response.HolidayRightsResults.NiszNumber == 78121925128;

All I’m getting is an error.

Hi @Coxjeffrey,
You seem to be accessing NiszNumber incorrectly. HolidayRightsResults is an array of objects. So you should be accessing it this way instead, response.HolidayRightsResults[0].NiszNumber

Additionally, you can use the newer syntax to write tests and use Lodash (in built in Postman sandbox) to access properties in objects

pm.test('Nisznumber is 78121925128', function () {
  expect(_.get(response, 'HolidayRightsResults.0.NiszNumber')).to.eql(78121925128);
});

https://learning.getpostman.com/docs/postman/scripts/test_scripts/
https://learning.getpostman.com/docs/postman/scripts/postman_sandbox/

1 Like

I also wrote something about accessing different parts of a JSON response that might be useful: