How to get count of number of nested instances

I have a json response like this:

"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

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

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).

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).

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

Something like the following?

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

image

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

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

javascript - How can I check if an object is an array? - Stack Overflow

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

Or in a test, you can do the following…

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.

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

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')