How to assert json with empty array

The following paste contains data returned by our API.

https://www.paste.org/107234

{
    "result": "OK",
    "sysStatisticsReport": null,
    "sysStatisticsData": {
        "sysDataStatisticsList": [{
            "RLibID":"x400410000",
            "sysDataDetails": []
        }, {
            "RLibID":"x872690000",
            "sysDataDetails": []
        }, {
            "RLibID":"x871710000",
            "sysDataDetails": []
        }, {
            "RLibID":"x800070000",
            "sysDataDetails": [{
                "sysvRequestId": 2811,
                "sysRequestDataStatistics": {
                    "requestingRLibID":"x800070000",
                    "requestingItsRequestId": "706742005###CG",
                    "availabilityRequestedAt": "2020-06-22 06:51:58",
                    "recordIdentifierType": "PPN",
                    "recordIdentifierCode": "321528360"
                },
                "sysReservedDataStatistics": [{
                    "requestingRLibID":"x800070000",
                    "requestingItsRequestId": "706742005###CG",
                    "ilsReservedAt": "2020-06-22 06:52:00",
                    "recordIdentifierType": "PPN",
                    "recordIdentifierCode": "321528360",
                    "xCodeWhereReserved": "x870410000",
                    "levelType": "COUNTRY",
                    "ilsCancelledMessage": "BY_TIMEOUT",
                    "reservationCancellationMessage": "BY_TIMEOUT",
                    "cancelledByReservedIts": false,
                    "cancelledBySYSVEngine": false,
                    "timeOutBySYSV": true
                }, {
                    "requestingRLibID":"x800070000",
                    "requestingItsRequestId": "706742005###CG",
                    "ilsReservedAt": "2020-06-22 06:52:00",
                    "recordIdentifierType": "PPN",
                    "recordIdentifierCode": "321528360",
                    "xCodeWhereReserved": "x834240000",
                    "levelType": "COUNTRY",
                    "ilsCancelledMessage": "BY_TIMEOUT",
                    "reservationCancellationMessage": "BY_TIMEOUT",
                    "cancelledByReservedIts": false,
                    "cancelledBySYSVEngine": false,
                    "timeOutBySYSV": true
                }, {
                    "requestingRLibID":"x800070000",
                    "requestingItsRequestId": "706742005###CG",
                    "ilsReservedAt": "2020-06-22 06:52:00",
                    "recordIdentifierType": "PPN",
                    "recordIdentifierCode": "321528360",
                    "xCodeWhereReserved": "x870290000",
                    "levelType": "COUNTRY",
                    "ilsCancelledMessage": "BY_TIMEOUT",
                    "reservationCancellationMessage": "BY_TIMEOUT",
                    "cancelledByReservedIts": false,
                    "cancelledBySYSVEngine": false,
                    "timeOutBySYSV": true
                }],
                "sysShippedDataStatistics": []
            }]
        }],
        "problem": null
    }
}

The first assertion I’m running is to see if a unique ID (contains ###CG) can be found like so (uid should be found in sysRequestDataStatistics. then, uid should be found in sysReservedDataStatistics, etc):

pm.test("Get Stats", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.
    sysStatisticsData.
    sysDataStatisticsList.
    sysDataDetails.
    sysRequestDataStatistics.
    requestingItsRequestId.
    value).
    to.eql(pm.collectionVariables.get("uID"));
});

However, the assertion fails at the detection of the first existing sysDataDetails being an empty array.

TypeError: Cannot read property ‘sysRequestDataStatistics’ of undefined

How should I create the correction assertion?

Hey @SparklingGrey,

Welcome to the community!! :rocket:

From the example response, it looks like a couple of those properties are arrays so you would need to either loop through them or specify which object you wanted.

For example:

pm.expect(jsonData.sysStatisticsData.sysDataStatisticsList[3].sysDataDetails[0].sysRequestDataStatistics.requestingItsRequestId).to.eql(pm.collectionVariables.get("uID"))

You don’t need to add .value to the end as the end of that chain would be the value for the requestingItsRequestId property.

I think it might be worth checking out this awesome tutorial from @vdespa on writing assertions against JSON responses:

2 Likes

Thank you for your detailed reply. Enough homework for me, and I will be back if I can’t figure it out.

Thanks again, much appreciated!

1 Like