0
0
Postmantesting~15 mins

Nested object assertions in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify nested object values in API response
Preconditions (2)
Step 1: Send a GET request to https://api.example.com/user/123
Step 2: Check the response status code is 200
Step 3: Verify the response body contains a 'user' object
Step 4: Inside 'user', verify 'id' equals 123
Step 5: Inside 'user', verify 'profile' object exists
Step 6: Inside 'profile', verify 'name' equals 'John Doe'
Step 7: Inside 'profile', verify 'address' object exists
Step 8: Inside 'address', verify 'city' equals 'New York'
Step 9: Inside 'address', verify 'zip' equals '10001'
✅ Expected Result: The API response status is 200 and all nested object values match the expected data.
Automation Requirements - Postman test scripts (JavaScript)
Assertions Needed:
Status code is 200
'user' object exists
'user.id' equals 123
'user.profile' object exists
'user.profile.name' equals 'John Doe'
'user.profile.address' object exists
'user.profile.address.city' equals 'New York'
'user.profile.address.zip' equals '10001'
Best Practices:
Use pm.response.to.have.status for status code assertion
Use pm.expect with deep property access for nested assertions
Check object existence before accessing nested properties
Write clear and descriptive assertion messages
Automated Solution
Postman
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

const jsonData = pm.response.json();

pm.test('Response has user object', () => {
    pm.expect(jsonData).to.have.property('user');
});

pm.test('User id is 123', () => {
    pm.expect(jsonData.user).to.have.property('id', 123);
});

pm.test('User profile object exists', () => {
    pm.expect(jsonData.user).to.have.property('profile');
});

pm.test('User profile name is John Doe', () => {
    pm.expect(jsonData.user.profile).to.have.property('name', 'John Doe');
});

pm.test('User profile address object exists', () => {
    pm.expect(jsonData.user.profile).to.have.property('address');
});

pm.test('User profile address city is New York', () => {
    pm.expect(jsonData.user.profile.address).to.have.property('city', 'New York');
});

pm.test('User profile address zip is 10001', () => {
    pm.expect(jsonData.user.profile.address).to.have.property('zip', '10001');
});

This script uses Postman test scripts written in JavaScript to automate the manual test case.

First, it checks the HTTP status code is 200 using pm.response.to.have.status(200).

Then, it parses the JSON response body with pm.response.json() and stores it in jsonData.

Each pm.test block verifies a specific nested property. It first checks if the parent object exists before accessing child properties to avoid errors.

Assertions use pm.expect(...).to.have.property(...) to check both existence and exact values.

This approach ensures clear, step-by-step validation of nested objects, making debugging easier if a test fails.

Common Mistakes - 3 Pitfalls
Accessing nested properties without checking if parent objects exist
{'mistake': 'Using incorrect assertion syntax like pm.expect(jsonData.user.id == 123)', 'why_bad': 'This does not use the assertion library properly and may not fail the test correctly.', 'correct_approach': "Use pm.expect(jsonData.user).to.have.property('id', 123) for clear and reliable assertions."}
Not verifying the response status code before checking body content
Bonus Challenge

Now add data-driven testing with 3 different user IDs: 123, 456, and 789, verifying their respective nested profile data.

Show Hint