46

I have a control that returns 2 records:

{
  "value": [
    {
      "ID": 5,
      "Pupil": 1900031265,
      "Offer": false,
    },
    {
      "ID": 8,
      "Pupil": 1900035302,
      "Offer": false,
      "OfferDetail": ""
    }
  ]
}

I need to test via Postman, that I have 2 records returned. I've tried various methods I've found here and elsewhere but with no luck. Using the code below fails to return the expected answer.

responseJson = JSON.parse(responseBody);
var list = responseBody.length;
tests["Expected number"] = list === undefined || list.length === 2;

At this point I'm not sure if it's the API I'm testing that's at fault or my coding - I've tried looping through the items returned but that's not working for me either. Could someone advise please - I'm new to javascript so am expecting there to be an obvious cause to my problem but I'm failing to see it. Many thanks.

Chrissi
  • 565
  • 1
  • 6
  • 6
  • `responseJson` is a object read the key `value` and then `length` – Kaushik Mar 14 '16 at 12:04
  • Your json array has a length of 1 object named "value" but your "value" object should have a length of 2. Check `responseJson.value` length instead. – c00ki3s Mar 14 '16 at 12:09

13 Answers13

51

In postman, under Tests section, do the following (screenshot below): var body = JSON.parse(responseBody); tests["Count: " + body.value.length] = true;

Here is what you should see (note: I replaced responseBody with JSON to mock up example above): enter image description here

RC_02
  • 3,146
  • 1
  • 18
  • 20
  • 1
    I add this to a large response and it never returns, just sits saying Loading for hours and hours. – Connie DeCinko Aug 15 '17 at 14:54
  • 5
    @ConnieDeCinko I have done this for large data and this code works for me ```var body = JSON.parse(responseBody); tests["Count: " + body.length] = true;``` – Hamid R. Jul 19 '19 at 11:44
27

Correct your json. and try this.

=======================v

var test = JSON.parse('{"value": [{"ID": 5,"Pupil": 1900031265,"Offer": false},{"ID": 8,"Pupil": 1900035302,"Offer": false,"OfferDetail": ""}] }')
    
test.value.length; // 2

So you need to identify the array in the json (starting with the [ bracket. and then take the key and then check the length of the key.

Kaushik
  • 2,072
  • 1
  • 23
  • 31
24

Here's the simplest way I figured it out:

pm.expect(Object.keys(pm.response.json()).length).to.eql(18);

No need to customize any of that to your variables. Just copy, paste, and adjust "18" to whatever number you're expecting.

Forrest
  • 2,968
  • 1
  • 27
  • 18
9

This is what I did for counting the recods

//parsing the Response body to a variable
    responseJson = JSON.parse(responseBody);

//Finding the length of the Response Array
    var list = responseJson.length;
    console.log(list);
    tests["Validate service retuns 70 records"] = list === 70;

enter image description here

Rakesh133
  • 351
  • 1
  • 5
  • 16
9

More updated version of asserting only 2 objects in an array:

pm.test("Only 2 objects in array", function (){
    pm.expect(pm.response.json().length).to.eql(2);
});
SweetNoods
  • 91
  • 1
  • 2
6

Your response body is an object you cannot find the length of an object try

var list = responseJson.value.length;
Mustafa Mamun
  • 2,591
  • 2
  • 14
  • 17
2

First of all you should convert response to json and find value path. Value is array. You should call to length function to get how many objects in there and check your expected size

pm.test("Validate value count", function () {
    pm.expect(pm.response.json().value.length).to.eq(2);
});
wallance
  • 325
  • 1
  • 10
1

I had a similar problem, what I used to test for a certain number of array members is:

responseJson = JSON.parse(responseBody);
tests["Response Body = []"] = responseJson.length === valueYouAreCheckingFor;

To check what values you're getting, print it and check the postman console.

console.log(responseJson.length);
0

Counting records in JSON array using javascript and insomnia

//response insomnia
const response = await insomnia.send();

//Parse Json
const body = JSON.parse(response.data);

//Print console:
console.log(body.data.records.length);
brunorojo
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 17 '22 at 22:23
0

 pm.test("Only 2 objects in array", function (){
     var jsonData = pm.response.json();
     let event_length = jsonData.data.length;
    pm.expect(event_length).to.eql(2);
 });
-1

As mentioned in the comments, you should test responseJson.value.length

responseJson = JSON.parse(responseBody); tests["Expected number"] = typeof responseJson === 'undefined' || responseJson.value.length;

kartsims
  • 1,001
  • 9
  • 16
-1

I was facing similar issue while validating the length of an array inside a JSON. The below snippet should help you resolve it-

responseJson = JSON.parse(responseBody);
var list = responseBody.length;
tests["Expected number"] = responseJson.value.length === list;
-1

Working Code

 pm.test("Verify the number of records",function()
 {
   var response = JSON.parse(responseBody); 
   pm.expect(Object.keys(response.value).length).to.eql(5);

 });
//Please change the value in to.eql function as per your requirement    
//'value' is the JSON notation name for this example and can change as per your JSON
Erty Seidohl
  • 4,487
  • 3
  • 33
  • 45