# How to assert json with empty array

**URL:** <https://community.postman.com/t/how-to-assert-json-with-empty-array/13719>\
**Category:** Help Hub\
**Created:** [June 29, 2020, 7:22am UTC](https://community.postman.com/t/how-to-assert-json-with-empty-array/13719 "2020-06-29T07:22:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SparklingGrey](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@SparklingGrey](https://community.postman.com/u/SparklingGrey)\
**Post date:** [June 29, 2020, 7:22am UTC](https://community.postman.com/t/how-to-assert-json-with-empty-array/13719/1 "2020-06-29T07:22:59Z")

</div>

The following paste contains data returned by our API.

> **[Paste code - paste.org](https://www.paste.org/107234)**
>
> www.paste.org - allows users to paste snippets of text, usually samples of source code, for public viewing. A place where lack of code gets binned; sharing code iterations since 2006.

```
{
    "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?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [June 29, 2020, 9:17am UTC](https://community.postman.com/t/how-to-assert-json-with-empty-array/13719/2 "2020-06-29T09:17:37Z")

</div>

Hey @SparklingGrey,

Welcome to the community!! 🚀

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:

[![](https://img.youtube.com/vi/NEO8RqnfYec/maxresdefault.jpg "How to write assertions in Postman (objects, arrays, nested properties)") ](https://www.youtube.com/watch?v=NEO8RqnfYec)

---

<div class="post-metadata">

**Author:** ![SparklingGrey](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@SparklingGrey](https://community.postman.com/u/SparklingGrey)\
**Post date:** [June 29, 2020, 9:36am UTC](https://community.postman.com/t/how-to-assert-json-with-empty-array/13719/3 "2020-06-29T09:36:07Z")

</div>

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!
