# How to count object in JSON?

**URL:** <https://community.postman.com/t/how-to-count-object-in-json/53921>\
**Category:** Help Hub\
**Tags:** tests, json, response\
**Created:** [October 26, 2023, 1:45pm UTC](https://community.postman.com/t/how-to-count-object-in-json/53921 "2023-10-26T13:45:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![payload-meteorologi2](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@payload-meteorologi2](https://community.postman.com/u/payload-meteorologi2)\
**Post date:** [October 26, 2023, 1:45pm UTC](https://community.postman.com/t/how-to-count-object-in-json/53921/1 "2023-10-26T13:45:56Z")

</div>

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!!!

 ![Zrzut ekranu 2023-10-26 150039](https://us1.discourse-cdn.com/getpostman/original/3X/6/6/665c63c717e1a58786daf2d43d17896f3ae4b79f.png)

---

<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:** [October 26, 2023, 1:52pm UTC](https://community.postman.com/t/how-to-count-object-in-json/53921/2 "2023-10-26T13:52:11Z")

</div>

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)](https://www.w3schools.com/jsref/jsref_length_array.asp)

```auto
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.

---

<div class="post-metadata">

**Author:** ![payload-meteorologi2](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@payload-meteorologi2](https://community.postman.com/u/payload-meteorologi2)\
**Post date:** [October 26, 2023, 3:29pm UTC](https://community.postman.com/t/how-to-count-object-in-json/53921/3 "2023-10-26T15:29:25Z")

</div>

This works perfect 🙂

var body = JSON.parse(responseBody);

tests["Array length is " + body.data.ib\_clients.length] = true;

![Zrzut ekranu 2023-10-26 172830](https://us1.discourse-cdn.com/getpostman/original/3X/2/4/24ff4c08f558c57c9eb94219231a644b9d72f6b6.png)

Thanks for Your support 🙂

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [October 29, 2023, 3:30pm UTC](https://community.postman.com/t/how-to-count-object-in-json/53921/4 "2023-10-29T15:30:10Z")

</div>

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