How to count length of response in new syntax

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!

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

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 in case you would like to check it out. Hope this helps.

5 Likes

@deepak.pathania thank you

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

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"
    }
]

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

Hello @starPilot Welcome to the Community :bouquet:

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

in your case it should be

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

1 Like

You could also use Lodash to find that length:

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

https://lodash.com/docs/4.17.15#size

3 Likes

Works great!

Thank you

StarPilot

1 Like

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

Fork it and try it out!

1 Like

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

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.

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

1 Like

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