# How to get count of number of nested instances

**URL:** <https://community.postman.com/t/how-to-get-count-of-number-of-nested-instances/48512>\
**Category:** Help Hub\
**Tags:** tests, scripts\
**Created:** [June 14, 2023, 8:50am UTC](https://community.postman.com/t/how-to-get-count-of-number-of-nested-instances/48512 "2023-06-14T08:50:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmudge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@dmudge](https://community.postman.com/u/dmudge)\
**Post date:** [June 14, 2023, 8:50am UTC](https://community.postman.com/t/how-to-get-count-of-number-of-nested-instances/48512/1 "2023-06-14T08:50:11Z")

</div>

I have a json response like this:

```auto
"TOP LEVEL KEYNAME1": "SOME DATA",
"TOP LEVEL KEYNAME2": {
        "2ND LEVEL KEYNAME 1": 2,
        "2ND LEVEL KEYNAME 2": "SOME DATA",
        "2ND LEVEL KEYNAME 3": [{
            "3RD LEVEL KEYNAME 1": "SOME DATA",
            "3RD LEVEL KEYNAME 2": "SOME DATA",
            "3RD LEVEL KEYNAME 3": "SOME DATA",
        }, {
            "3RD LEVEL KEYNAME 1": "SOME DATA",
            "3RD LEVEL KEYNAME 2": "SOME DATA",
            "3RD LEVEL KEYNAME 3": "SOME DATA",
        }]

```

so it has 2 top level keys, inside key 2, the first key is actually a count of how many instances there are of the 2nd level (2)  
So this checks out because the 3rd key of the 2nd level has two instances each with 3 keys)

What I want to get is the count of how many instances there are which is 3

I can get the count of how many top level keys there are:  
`jsonTopLevelLength = Object.keys(responseJson).length; = 2`

I can get the count of how many 2nd level keys there are:  
`jsonSecondLevelSecondKeyLength = Object.keys(responseJson.TOP LEVEL KEYNAME2).length; = 3`

I can even get the count in each of the third level keys there in each instance

```auto
jsonThirdLevelSecondKeyLength = Object.keys(responseJson.TOP LEVEL KEYNAME2.2ND LEVEL KEYNAME 3[0]).length
&
jsonThirdLevelSecondKeyLength = Object.keys(responseJson.TOP LEVEL KEYNAME2.2ND LEVEL KEYNAME 3[1]).length

```

Both equal 3 but I just can’t seem to find how do I get a count of for example: `Object.keys(responseJson.TOP LEVEL KEYNAME2.2ND LEVEL KEYNAME 3).length`

doesnt work as it want to count how many keys there are under 2ND LEVEL KEYNAME 3 and as there are 2 of them it wants me to supply [0] or [1] Thanks a lot

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 14, 2023, 10:56am UTC](https://community.postman.com/t/how-to-get-count-of-number-of-nested-instances/48512/2 "2023-06-14T10:56:53Z")

</div>

There are zero keys under “2ND LEVEL KEYNAME 3”.

There is an array, with two objects in it.

Each of those objects has 3 key\value pairs.

Semantics but its important to understand the distinction. The array itself doesn’t have any keys in this instance.

The following is targeting the objects within the array by its array index (array indexes start at 0).

```auto
Object.keys(responseJson.TOP LEVEL KEYNAME2.2ND LEVEL KEYNAME 3[0]).length

```

What are you expecting the result to be? 6? (In which case target the elements by their array indexes like you have done and add the two together).

---

<div class="post-metadata">

**Author:** ![dmudge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@dmudge](https://community.postman.com/u/dmudge)\
**Post date:** [June 14, 2023, 11:23am UTC](https://community.postman.com/t/how-to-get-count-of-number-of-nested-instances/48512/3 "2023-06-14T11:23:40Z")

</div>

ok thanks so they key value is an array.

What I am trying to achieve is to verify that both groups of 2ND LEVEL KEYNAME 3 each have those 3 keys inside

The test doesnt know how many groups there are (2) so I need someway to know how many groups of 2ND LEVEL KEYNAME 3 there are, in this case (2) so that I can then run a for loop over both and verify they both have the same 3 keys inside them?

Can this be done? as we have some requests that bring back more than 2 groups so we cant hard code the test

Thanks

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 14, 2023, 12:02pm UTC](https://community.postman.com/t/how-to-get-count-of-number-of-nested-instances/48512/4 "2023-06-14T12:02:41Z")

</div>

Something like the following?

```auto
const response = pm.response.json();

console.log(response["TOP LEVEL KEYNAME2"])
console.log(response["TOP LEVEL KEYNAME2"]["2ND LEVEL KEYNAME 3"])

let array = response["TOP LEVEL KEYNAME2"]["2ND LEVEL KEYNAME 3"];

array.forEach((obj, index) => {
    pm.test(`2ND LEVEL KEYNAME 3 - object index[${index}] = 3 keys`, () => {
        pm.expect(Object.keys(obj).length).to.eql(3);
    });
});

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/9/490e9a1253f4733a198267e09dfd434faf1e6a56.png)

![image](https://us1.discourse-cdn.com/getpostman/original/3X/1/a/1a2f1209f6460eda9ee3d28b34c55c23941a0792.png)

---

<div class="post-metadata">

**Author:** ![dmudge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@dmudge](https://community.postman.com/u/dmudge)\
**Post date:** [June 14, 2023, 12:30pm UTC](https://community.postman.com/t/how-to-get-count-of-number-of-nested-instances/48512/5 "2023-06-14T12:30:32Z")

</div>

thankyou so much! working now

Can I ask something else please how do I determine a Key Value is not an array?

Ie is just a single value

And so I want to test the length is 1

I tried

`const length = (responseJson.TOP LEVEL KEYNAME1).length`

or even

```auto
const array = responseJson.TOP LEVEL KEYNAME1
and then do an array.length

```

but both of these are returning the string length of the value not 1 as I want!

Cheers

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 14, 2023, 1:09pm UTC](https://community.postman.com/t/how-to-get-count-of-number-of-nested-instances/48512/6 "2023-06-14T13:09:53Z")

</div>

[javascript - How can I check if an object is an array? - Stack Overflow](https://stackoverflow.com/questions/4775722/how-can-i-check-if-an-object-is-an-array)

```auto
console.log(Object.prototype.toString.call(array)); // "[object Array]"

```

Or in a test, you can do the following…

```auto
pm.test(`array test`, () => {
    pm.expect(array).to.be.an("array").that.is.not.empty;    
});

```

A key can have a string as a value, an array, or contain other objects.

Just because its not an array, doesn’t mean it can’t have more than one element.

An array can have objects, or it can just have a list of values. (It doesn’t have to be key\value pairs within the array but often is).

Therefore, you still need to count the number of keys to find out how many elements are in an object.

```auto
console.log(Object.keys(response).length); // 2

```

---

<div class="post-metadata">

**Author:** ![dmudge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@dmudge](https://community.postman.com/u/dmudge)\
**Post date:** [June 14, 2023, 11:38pm UTC](https://community.postman.com/t/how-to-get-count-of-number-of-nested-instances/48512/7 "2023-06-14T23:38:02Z")

</div>

thanks yeah so reading it seems there really is no way to to get the length of 1 when the key value is a single string or integer

So what I came up with is to verify the key value is a string or a number that returns true and if I run it against a key that has an array of values, it fails

Cheers

`pm.expect(objectValue).to.be.a('string') || pm.expect(objectValue).to.be.a('number')`

---

<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:** [September 30, 2023, 8:03am UTC](https://community.postman.com/t/how-to-get-count-of-number-of-nested-instances/48512/8 "2023-09-30T08:03:11Z")

</div>


