# How to count length of response in new syntax

**URL:** <https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [May 3, 2018, 2:14pm UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539 "2018-05-03T14:14:01Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![captain\_hoy](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@captain\_hoy](https://community.postman.com/u/captain_hoy)\
**Post date:** [May 3, 2018, 2:14pm UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539/1 "2018-05-03T14:14:02Z")

</div>

I am struggling with the new syntax and would need your input. How can you write below test with pm? (the new syntax)

responseJson = JSON.parse(responseBody);  
tests["Nr of plans returned = " +responseJson.length] = responseJson.length === 2;

I appreciate your help!

---

<div class="post-metadata">

**Author:** ![deepak.pathania](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@deepak.pathania](https://community.postman.com/u/deepak.pathania)\
**Post date:** [May 7, 2018, 9:26am UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539/2 "2018-05-07T09:26:44Z")

</div>

Hey @captain_hoy, here’s how you can achieve what you want with the new `pm.*` syntax.

```auto
var data = pm.response.json();

pm.test('Number of plans returned = ' + data.length, function () {
    pm.expect(data.length).to.equal(2);
});

```

In case you want to check the count of keys in a JSON object, simply replace `data` with `Object.keys(data)` and you’ll be good to go.

Also, you can access the complete API reference [here](https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference) in case you would like to check it out. Hope this helps.

---

<div class="post-metadata">

**Author:** ![dutq](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/dutq/32/2825_2.png) [@dutq](https://community.postman.com/u/dutq)\
**Post date:** [September 17, 2019, 3:53am UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539/3 "2019-09-17T03:53:21Z")

</div>

@deepak.pathania thank you

---

<div class="post-metadata">

**Author:** ![DineshkumarRavi](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@DineshkumarRavi](https://community.postman.com/u/DineshkumarRavi)\
**Post date:** [April 23, 2020, 6:21am UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539/4 "2020-04-23T06:21:34Z")

</div>

hi, how can i get the count and name of the response body in API POSTMAN ,PLEASE GIVE ME A SUGGESTION.

---

<div class="post-metadata">

**Author:** ![krishna81m](https://avatars.discourse-cdn.com/v4/letter/k/5e9695/32.png) [@krishna81m](https://community.postman.com/u/krishna81m)\
**Post date:** [December 23, 2020, 2:06am UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539/5 "2020-12-23T02:06:50Z")

</div>

new to this syntax, what about if the json had no root, how do you use the Json as Json Array length: pm.test(`Count: ${pm.response.json().length}`) seems to only look at the entire json?

Json response without root:

```
[
    {
        "id": "1"
    },{
        "id": "2"
    }
]
```

---

<div class="post-metadata">

**Author:** ![starPilot](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@starPilot](https://community.postman.com/u/starPilot)\
**Post date:** [February 12, 2021, 4:39pm UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539/6 "2021-02-12T16:39:57Z")

</div>

Hello Deepak

I am trying to get the number of keys in the response body.

You wrote:

In case you want to check the count of keys in a JSON object, simply replace `data` with `Object.keys(data)` and you’ll be good to go.

I am not clear about this. I tried:

const responseJson = pm.response.json();

console.log("The number of expected keys in the response body is: " + Object.keys(data));

That does not work.

What am I doing wrong?

Thanks  
Phil

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [February 13, 2021, 2:17am UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539/7 "2021-02-13T02:17:12Z")

</div>

Hello @starPilot Welcome to the Community 💐

In the above example he is storing the parsed response under the variable “data”

in your case it should be

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

var count = Object.keys(responseJson).length;

console.log("The number of expected keys in the response body is: " + count);

```

Please let me know if this works:-)

---

<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:** [February 13, 2021, 8:17am UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539/8 "2021-02-13T08:17:05Z")

</div>

You could also use `Lodash` to find that length:

```javascript
console.log(`Number of keys: ${_.size(pm.response.json())}`)

```

[https://lodash.com/docs/4.17.15#size](https://lodash.com/docs/4.17.15#size)

---

<div class="post-metadata">

**Author:** ![starPilot](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@starPilot](https://community.postman.com/u/starPilot)\
**Post date:** [February 14, 2021, 10:06am UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539/9 "2021-02-14T10:06:57Z")

</div>

Works great!

Thank you

StarPilot

---

<div class="post-metadata">

**Author:** ![arlem](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/arlem/32/3078_2.png) [@arlem](https://community.postman.com/u/arlem)\
**Post date:** [March 19, 2021, 11:51am UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539/10 "2021-03-19T11:51:27Z")

</div>

Here’s a collection demonstrating how you can either count the length of an array, or count the number of keys in a specified array:

[https://www.postman.com/postman/workspace/postman-answers/collection/9215231-6d1bb03a-5a0a-4ec4-ab9c-d79c01ac96d7?ctx=documentation](https://www.postman.com/postman/workspace/postman-answers/collection/9215231-6d1bb03a-5a0a-4ec4-ab9c-d79c01ac96d7?ctx=documentation)

Fork it and try it out!

---

<div class="post-metadata">

**Author:** ![shivabagli](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@shivabagli](https://community.postman.com/u/shivabagli)\
**Post date:** [March 21, 2023, 10:59am UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539/12 "2023-03-21T10:59:39Z")

</div>

I forked it to try out but I was not able to send the request. Could you please help

---

<div class="post-metadata">

**Author:** ![shivabagli](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@shivabagli](https://community.postman.com/u/shivabagli)\
**Post date:** [March 21, 2023, 11:02am UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539/13 "2023-03-21T11:02:47Z")

</div>

Our API fetches total records/total books. Further it provides details such book id, book name, author name etc. as a array in response.  
Now I want to validate the pagination. Suppose the total books is equal to 768, and in the request body I have provided pageSize as 100 and pageNumber as 1, the response I get is something as below.

“totalBooks”: 768,  
“Books”:  
[  
Book1:{  
“bookName”: “abc”,  
“bookId”: “xyz”,  
},  
“Book2”:{  
“bookName”: “abcqw”,  
“bookId”: “xyzplpkj”,  
},  
“Book3”:{  
“bookName”: “abcpqr”,  
“bookId”: “xyzghj”,  
},  
.  
.  
.  
.  
.  
Book100: {  
“bookName”: “asfabc”,  
“bookId”: “xyzpoi”,  
}  
]

So ideally I should be getting 8 pages, first 7 pages should have 100 each and the last page should have 68. How can I validate the pagination that each page is having 100 books each.

Could you be able to help with above scenario.

---

<div class="post-metadata">

**Author:** ![arlem](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/arlem/32/3078_2.png) [@arlem](https://community.postman.com/u/arlem)\
**Post date:** [March 22, 2023, 1:24pm UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539/14 "2023-03-22T13:24:00Z")

</div>

@shivabagli I’d recommend opening a new topic since this is a different problem.

For what it’s worth the answer will highly depend on how the API is designed. Typically an API with pagination will give a token to the next page, if that’s the case for yours then you can loop over the request until there isn’t a `next` token anymore and keep count of the book items.

---

<div class="post-metadata">

**Author:** ![analuisa-campos](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@analuisa-campos](https://community.postman.com/u/analuisa-campos)\
**Post date:** [April 10, 2023, 1:53pm UTC](https://community.postman.com/t/how-to-count-length-of-response-in-new-syntax/1539/15 "2023-04-10T13:53:21Z")

</div>

hola,  
mi respuesta es:  
{  
“status”: “success”,  
“data”: {  
“id”: “1”,  
“name”: “TIENDA - ARICA”,  
“isOperacional”: true,  
“type”: “CUSTOMER STORE”,  
“address”: {  
“city”: “ARICA”,  
“state”: “XV REGIÓN”,  
“street”: “21 DE MAYO 466”,  
“number”: “458”,  
“postalCode”: “1000622”  
},  
“storeInformation”: {  
“isPickupInStore”: true,  
“isShipToStore”: false,  
“isShipFromStore”: false  
}  
}  
}

y en el test agrego el siguiente código:

```
**let jsonData = pm.response.json();**

```

\*\* pm.test(“número de registros retornados”, function(){\*\*  
\*\* pm.expect(jsonData.length).to.equal(3);\*\*

\*\* });\*\*

pero envía un mensaje de error: **número de registros retornados | AssertionError: expected undefined to equal 3**
