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