const jsonData = pm.response.json();
pm.test("Test data type of the response", () => {
pm.expect(jsonData).to.be.an("object");
//pm.expect(jsonData.name).to.be.a("string");
//pm.expect(jsonData.id).to.be.a("number");
//pm.expect(jsonData.courses).to.be.an("array");
});
Currently, the only way I could replicate your issue was to remove the first part ( { "students": ) and the closing } bracket of the JSONdata so that it was an array, like this:
Hi James Wadley, Thank you for a quick help on this issue. As per your suggestion i tried removing ( { "students": ) and the closing } bracket of the JSONdata.
//************JSON data modified as per your suggestion:
Can we see a screenshot of the actual response, just to double check that its correct.
Can you confirm if the tests are passing in Postman. Are they failing in Newman? Is that the issue?
The data looks fine. You can console log the type of a variable to aid in troubleshooting.
However, an array is a special type of object, so if you console log it, it will show as “object”. So maybe not so useful in this circumstance.
It all looks ok to me and the following tests are passing. (Based on your original response\JSON with the array).
const jsonData = pm.response.json();
console.log(jsonData);
pm.test("response to be an object", () => {
console.log(typeof(jsonData));
pm.expect(jsonData).to.be.an("object");
});
pm.test("response/students to be an array", () => {
console.log(typeof(jsonData.students));
pm.expect(jsonData.students).to.be.an("array");
});
One thing to note though is that your tests for the name, id and courses will fail as they are not targeting the array elements. You’ve got three records. Do you want to check those fields for all records? You can create a loop if you need to check each record.
I still recommend breaking your assertions into separate tests. In Postman, the first assertion in a test to fail will stop the rest of the code from executing.
Try and keep the assertions within a test to a minimum (Ideally singular).
Because the object test is failing and its the first assertion, you are not getting the feedback on the following assertions until you fix the first assertion.
I didn’t recommend removing the first part of your JSON, I said it was the only way I could replicate your issue.
My suggestion was to check that your JSON is as you believe it to be.
when I copy-paste your example, it works.
This would suggest that you are submitting something different to what you have shared in your example above.