Test Overview
This test sends a GET request to an API endpoint and verifies nested objects in the JSON response. It checks that the user details and address fields exist and have expected values.
This test sends a GET request to an API endpoint and verifies nested objects in the JSON response. It checks that the user details and address fields exist and have expected values.
pm.test("Verify nested user details and address", function () { const jsonData = pm.response.json(); pm.expect(jsonData).to.have.property('user'); pm.expect(jsonData.user).to.have.property('name', 'John Doe'); pm.expect(jsonData.user).to.have.property('address'); pm.expect(jsonData.user.address).to.include({ city: 'New York', zip: '10001' }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Postman test runner is ready | — | PASS |
| 2 | Send GET request to API endpoint | API server receives request and sends JSON response with user details | — | PASS |
| 3 | Parse JSON response body | Response body parsed into jsonData object | — | PASS |
| 4 | Check if 'user' property exists in jsonData | jsonData contains 'user' object | jsonData has property 'user' | PASS |
| 5 | Check if 'name' property of user equals 'John Doe' | jsonData.user.name is 'John Doe' | jsonData.user.name === 'John Doe' | PASS |
| 6 | Check if 'address' property exists in user object | jsonData.user.address object exists | jsonData.user has property 'address' | PASS |
| 7 | Check if 'city' and 'zip' in address match expected values | jsonData.user.address.city is 'New York' and zip is '10001' | jsonData.user.address includes {city: 'New York', zip: '10001'} | PASS |
| 8 | Test ends with all assertions passing | All nested object assertions verified successfully | — | PASS |
user.name in a JSON response for assertion?object.property.user.name, use pm.expect(jsonData.user.name) which correctly accesses the nested value.response.body.data.user.age equals 30 in Postman?to.eql(value) checks deep equality including numbers.to.eql(30) with correct arrow function and property access, matching expected number type.{"order": {"id": 101, "items": [{"name": "Pen", "qty": 3}, {"name": "Book", "qty": 2}]}}pm.test('Check first item qty', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.order.items[0].qty).to.eql(3);
});items with first element having qty: 3.items[0].qty and asserts it equals 3, which matches the JSON data.pm.test('Check user city', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.user.address.city).to.eql('New York');
});TypeError: Cannot read property 'city' of undefined. What is the likely cause?Cannot read property 'city' of undefined, meaning address is undefined.jsonData.user.address is missing, trying to access .city causes this error.{"users": [{"id": 1, "profile": {"name": "Alice", "active": true}}, {"id": 2, "profile": {"name": "Bob", "active": false}}]}profile.active property set to true. Which code snippet correctly performs this nested object assertion?profile.active is true.profile.active equals true, which is correct and clear.users.profile.active which is invalid.every() on users[0] (an object), causing TypeError as objects have no every method.user.profile equals true, but profile is an object not equal to true and misses .active.Pass the test and this pattern moves unknown