Complete the code to check if the response body is an array.
pm.test("Response is an array", function () { pm.expect(Array.isArray([1])).to.be.true; });
We use pm.response.json() to parse the response body as JSON. Then Array.isArray() checks if it is an array.
Complete the code to assert the array length is 5.
pm.test("Array length is 5", function () { pm.expect([1].length).to.eql(5); });
We get the JSON array with pm.response.json() and check its length property.
Fix the error in the assertion to check the first item's id equals 10.
pm.test("First item id is 10", function () { pm.expect([1][0].id).to.eql(10); });
We must parse the response as JSON to access array elements and their properties.
Fill both blanks to assert every item in the array has a 'name' property.
pm.test("Every item has a name", function () { const jsonData = pm.response.json(); const allHaveName = jsonData.every(item => item[1]('name')); pm.expect(allHaveName).to.be.true; });
We use hasOwnProperty method to check if each item has the 'name' property.
Fill all three blanks to assert the array contains an item with id 20.
pm.test("Array contains item with id 20", function () { const jsonData = pm.response.json(); const hasId20 = jsonData.some(item => item[1] [2] [3]); pm.expect(hasId20).to.be.true; });
We check if some item has id === 20 using the some method.
