1

Please assist in getting the property type of a nested element from a JSON response in postman. Below is my response after doing a POST.

{
    "MyList": [
        [
            {
                "id": 1,
                "name": "Test"
            }
        ]
    ]
}

I would like to check the name and id properties if they are of type number and string. Below is my code but getting error : Cannot read property '0' of undefined.

 pm.test("Check schema and datatype", () =>{
    var jsonData = pm.response.json();

    pm.expect(typeof(jsonData[0].id)).to.eql('number');
    pm.expect(typeof(jsonData[0].name)).to.eql('string');
 })
shayan rok rok
  • 494
  • 3
  • 12
  • You expected `jsonData` to be a list of objects but here `jsonData` is an `undefined` type, not a list of objects. – shayan rok rok Mar 06 '20 at 09:40
  • Thanks for your reply @shayanrokrok , How would I go passed this issue:( – King Don father Mar 06 '20 at 09:42
  • You can either check who should fill the jsonData or put a snippet code in question showing who should fill the `jsonData` of the list of objects. – shayan rok rok Mar 06 '20 at 09:48
  • So basically there is no other way to check the properties of the response output above? @shayanrokrok – King Don father Mar 06 '20 at 09:50
  • This modification you applied made your question so much better. First check `pm.response`. Maybe the response can't be converted from JSON. – shayan rok rok Mar 06 '20 at 10:05
  • Check https://github.com/postmanlabs/postman-app-support/issues/6507 . The [maintainers said](https://github.com/postmanlabs/postman-app-support/issues/6507#issuecomment-503402278) the `empty response` bug is fixed on v2.1.0 – shayan rok rok Mar 06 '20 at 11:23
  • Also, make sure you are running the script in the `Tests` tab in Postman. – shayan rok rok Mar 06 '20 at 11:51
  • Is that the actual response body, it doesn't look right to me. You have an array inside the array. You would also need to use `jsonData.MyList[0].id` - this isn't a Postman problem, more of an understanding of JSON and JS problem. – Danny Dainton Mar 06 '20 at 13:18

1 Answers1

0

Although your response body looks a little strange to me, you could create a test to check those data types like this:

pm.test("Check schema and datatype", () => {
    let jsonData = pm.response.json();

    pm.expect(jsonData.MyList[0][0].id).to.be.a('number');
    pm.expect(jsonData.MyList[0][0].name).to.be.a('string');
})

This is using the chai a method:

https://www.chaijs.com/api/bdd/#method_a

EDIT

To check the size or number of objects in the array, you can use this:

pm.test("Verify number of records returned", () => { 
    let jsonData = pm.response.json().MyList[0]
    pm.expect(jsonData.length).to.equal(1);
});
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • Hi, How would I go about counting the records returned for the response above? I have something like this now but seems to not work. pm.test("Verify that records returned is 31", () => { var jsonData = JSON.parse(responseBody); var size = jsonData.length; console.log(size); pm.expect(size).to.equal(31); }); – King Don father Mar 19 '20 at 09:12
  • I've edited the answer to check the number of objects in the array. You should really have opened a separate question for this. – Danny Dainton Mar 19 '20 at 11:12