Nested object assertions in Postman - Build an Automation Script
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.
Now add data-driven testing with 3 different user IDs: 123, 456, and 789, verifying their respective nested profile data.
Practice
user.name in a JSON response for assertion?Solution
Step 1: Understand JSON nested property access
In JavaScript, nested properties are accessed using dot notation likeobject.property.Step 2: Apply correct syntax in Postman test
To assertuser.name, usepm.expect(jsonData.user.name)which correctly accesses the nested value.Final Answer:
Use pm.expect(jsonData.user.name) to assert the nested value. -> Option BQuick Check:
Dot notation = pm.expect(jsonData.user.name) [OK]
- Using brackets incorrectly for nested keys
- Using hyphens instead of dots for nested access
- Missing quotes around string keys in bracket notation
response.body.data.user.age equals 30 in Postman?Solution
Step 1: Identify correct assertion method
Postman uses Chai assertion library whereto.eql(value)checks deep equality including numbers.Step 2: Check syntax correctness
pm.test('Age is 30', () => { pm.expect(response.body.data.user.age).to.eql(30); }); usesto.eql(30)with correct arrow function and property access, matching expected number type.Final Answer:
pm.test('Age is 30', () => { pm.expect(response.body.data.user.age).to.eql(30); }); -> Option AQuick Check:
Use to.eql for value equality in Postman tests [OK]
- Using string '30' instead of number 30
- Using incorrect assertion methods like to.be()
- Missing arrow function syntax in pm.test
{"order": {"id": 101, "items": [{"name": "Pen", "qty": 3}, {"name": "Book", "qty": 2}]}}What will this Postman test output?
pm.test('Check first item qty', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.order.items[0].qty).to.eql(3);
});Solution
Step 1: Analyze JSON structure and access
The JSON has an arrayitemswith first element havingqty: 3.Step 2: Check assertion correctness
The test accessesitems[0].qtyand asserts it equals 3, which matches the JSON data.Final Answer:
Test will pass because first item's qty is 3. -> Option AQuick Check:
Array index 0 qty = 3 matches assertion [OK]
- Confusing array index starting at 1 instead of 0
- Assuming items is an object, not an array
- Misreading qty values in nested objects
pm.test('Check user city', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.user.address.city).to.eql('New York');
});But the test fails with error:
TypeError: Cannot read property 'city' of undefined. What is the likely cause?Solution
Step 1: Understand the error message
The error saysCannot read property 'city' of undefined, meaningaddressis undefined.Step 2: Identify cause in nested access
IfjsonData.user.addressis missing, trying to access.citycauses this error.Final Answer:
The address property is missing or undefined in user. -> Option DQuick Check:
Undefined parent property causes TypeError in nested access [OK]
- Assuming all nested keys always exist
- Ignoring error message details
- Blaming assertion method instead of data structure
{"users": [{"id": 1, "profile": {"name": "Alice", "active": true}}, {"id": 2, "profile": {"name": "Bob", "active": false}}]}Write a Postman test to assert that all users have a
profile.active property set to true. Which code snippet correctly performs this nested object assertion?Solution
Step 1: Understand requirement to check all users' active status
We must assert each user'sprofile.activeis true.Step 2: Evaluate each option's approach
pm.test('All users active', () => { const users = pm.response.json().users; users.forEach(user => { pm.expect(user.profile.active).to.eql(true); }); }); loops over users and asserts eachprofile.activeequals true, which is correct and clear.
pm.test('All users active', () => { const users = pm.response.json().users; pm.expect(users.profile.active).to.eql(true); }); wrongly tries to accessusers.profile.activewhich is invalid.
pm.test('All users active', () => { const users = pm.response.json().users; pm.expect(users[0].every(user => user.profile.active)).to.eql(true); }); wrongly callsevery()onusers[0](an object), causing TypeError as objects have noeverymethod.
pm.test('All users active', () => { const users = pm.response.json().users; users.map(user => pm.expect(user.profile).to.eql(true)); }); assertsuser.profileequals true, butprofileis an object not equal to true and misses.active.Final Answer:
Use forEach loop to assert each user's profile.active is true. -> Option CQuick Check:
Loop and assert each nested property for all items [OK]
- Accessing nested property on array directly
- Calling array methods like every() on single objects
- Asserting entire object equals true instead of its boolean property
Ready to prove it?
Pass the test and this pattern moves unknown
