How to count object in JSON?

Hi guys.

Can anyone tell me how to write a test that will count how many objects I have nested in JSON in the response.
I don’t know the number, so “to.equal” is out of the question.
I would like Postman to return a number in the console.
PS.
I searched the forum, none of the answers worked.
Thanks!!!

Postman uses JavaScript, so I would recommend searching the web with JavaScript in the title.

You have an object called data, with an array of objects called ib_clients.

I assume you mean the number of objects in that array which you can use the array.length() method.

JavaScript Array length Property (w3schools.com)

const response = pm.response.json();
console.log((data.ib_clients).length);

This will return the number in the console logs, but I can’t see how you can craft an appropriate test for this unless you know what the expected number will be.

2 Likes

This works perfect :slight_smile:

var body = JSON.parse(responseBody);

tests["Array length is " + body.data.ib_clients.length] = true;

Zrzut ekranu 2023-10-26 172830

Thanks for Your support :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.