admin管理员组

文章数量:1164838

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.

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.

Share Improve this question asked Mar 14, 2016 at 12:02 ChrissiChrissi 6251 gold badge7 silver badges6 bronze badges 2
  • responseJson is a object read the key value and then length – Kaushik Commented Mar 14, 2016 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 Commented Mar 14, 2016 at 12:09
Add a comment  | 

13 Answers 13

Reset to default 58

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

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.

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.

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;

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);
});

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

var list = responseJson.value.length;

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);
});

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

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

 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);
 });

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

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

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;

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

本文标签: Counting records in JSON array using javascript and PostmanStack Overflow