Open In App

How to Count Records in JSON array using JavaScript and Postman ?

In this article, we are going to explore how JavaScript and Postman are used to count the number of records returned in a JSON array.

Here I have an API endpoint of my own that returns all the users registered in my application. We are going to use this API to count the number of records returned in the JSON response.



API Endpoint

Steps to Count records in JSON array using JavaScript and Postman:

var data = pm.response.json();

pm.test('Number of users returned = ' + data.length);

Check the test results

Testing the Count:

If we want to check something related to the count, like checking whether it is equal to 2 or not, we can also do that with the help of a script.

var data = pm.response.json();

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

Output:



Testing for only 2 records

Article Tags :