Tests, count response of nested obj array

I am trying to get the count of the item object in the below response. In my example it should count 3.

{
    "items": [
        {
            "id": 00001,
            "name": "TEST1"
        },
        {
            "id": 00002,
            "name": "TEST2"
        },
        {
            "id": 00003,
            "name": "TEST3"
        },
    ],
    "errors": null
}

Currently I have
tests["Count: " + (pm.response.json().items.length)] = true;

but this does not give the correct count when the items array gets too large.

By saying = true you are saying that the test will always pass.

If you’re looking to check the count against a specific value, your test should look more like:

tests["Items length is greater than or equal to 3"] = pm.response.json().items.length >= 3;

The format for writing tests in Postman has changed from this older syntax, so it’d be worth checking out how tests can be written now. It follows a chai pattern which might be more familiar to you:
https://learning.getpostman.com/docs/postman/scripts/test_examples

1 Like

Correct, I want my test to ALWAYS show as passed, its just getting the correct count of the nested array of objects that postman and I are struggling with.

I see. Are you able to provide more details on what the array looks like when it becomes ‘too large’ to be accurate? The functionality you are using is the built-in method of JS for determining length of a string/array, so it’s likely that the structure of the array at a larger size is not what you’re expecting. Perhaps it’s a 2D array rather than just an array of objects?

Structure is exactly how i posted just with a greater number of objects, ie. items array has 200 objects which also have around 20 key,values with some objects nested. I only want the count of objects in the items array, so below would still return 3:

"items": [
    {
        "id": 00001,
        "name": "TEST1",
        "anotherObject": {
                   "id": 1234,
                   "childName": "batman"
        },
       "updatedDateTime": "2018-10-08T00:00:00.0000000-05:00"
    },
    {
        "id": 00002,
        "name": "TEST2",
        "anotherObject": {
                   "id": 12345,
                   "childName": "superman"
        },
       "updatedDateTime": "2018-11-08T00:00:00.0000000-05:00"
    },
    {
        "id": 00003,
        "name": "TEST3",
        "anotherObject": {
                   "id": 123456,
                   "childName": "daredevil"
        },
       "updatedDateTime": "2018-12-08T00:00:00.0000000-05:00"
    },
],
"errors": null

}

Perhaps you can paste an example of the large array on https://pastebin.com/ so we can evaluate it? If you let us know how many items you think are in the array that will allow us to check…

Just to be clear, you aren’t expecting this to count recursively… i.e. count every object inside any other object too?

Correct not counting recursively, just the count of the objects in the items array up to 200

PasteBin here: https://pastebin.com/CPhB5Kph

There are 19 objects at the base level of that array. Each object is large - I don’t know if that’s where the confusion comes from?

Thanks Matt, my error, another question so how would i write that test so that it doesnt fail when the items object is null?

You could just not run that test if that’s the case:

const items = pm.response.json().items;

if (items && items.length) {
  tests["Count: " + items.length] = true;
} else {
  // items was null
}

or perhaps if you’d like it to come through with 0 you could do:

const items = pm.response.json().items;

tests["Count: " + ((items && items.length) || 0)] = true;