We use nested object assertions to check values inside objects within objects. This helps us make sure all parts of complex data are correct.
When testing API responses that have objects inside other objects.
When you want to verify a user's address details inside a user profile object.
When checking product details that include nested specifications.
When validating configuration settings that are grouped in nested objects.
Syntax
Postman
pm.test('Test name', function () {
pm.expect(responseJson.parent.child).to.eql(expectedValue);
});
Use dot notation to access nested properties.
Use pm.expect() with .to.eql() to compare values deeply.
Examples
This checks that the city inside the address object of user is 'New York'.
Postman
pm.test('Check user city', function () {
pm.expect(responseJson.user.address.city).to.eql('New York');
});
This verifies the price inside the details object of product is 29.99.
Postman
pm.test('Verify product price', function () {
pm.expect(responseJson.product.details.price).to.eql(29.99);
});
Sample Program
This test script checks two nested properties: city and zip inside the user's address object.
Postman
const responseJson = {
user: {
id: 1,
name: 'Alice',
address: {
street: '123 Main St',
city: 'Springfield',
zip: '12345'
}
}
};
pm.test('User city is Springfield', function () {
pm.expect(responseJson.user.address.city).to.eql('Springfield');
});
pm.test('User zip code is 12345', function () {
pm.expect(responseJson.user.address.zip).to.eql('12345');
});
OutputSuccess
Important Notes
Always check that the nested path exists before asserting to avoid errors.
Use descriptive test names to know which nested property failed if test fails.
Summary
Nested object assertions help verify deep data inside complex responses.
Use dot notation to access nested properties clearly.
Write clear tests to catch errors in specific nested fields.
Practice
(1/5)
1. In Postman, how do you access a nested property user.name in a JSON response for assertion?
easy
A. Use pm.expect(jsonData.user[name]) to access nested property.
B. Use pm.expect(jsonData.user.name) to assert the nested value.
C. Use pm.expect(jsonData['user-name']) to access nested property.
D. Use pm.expect(jsonData.user-name) to access nested property.
Solution
Step 1: Understand JSON nested property access
In JavaScript, nested properties are accessed using dot notation like object.property.
Step 2: Apply correct syntax in Postman test
To assert user.name, use pm.expect(jsonData.user.name) which correctly accesses the nested value.
Final Answer:
Use pm.expect(jsonData.user.name) to assert the nested value. -> Option B
Quick Check:
Dot notation = pm.expect(jsonData.user.name) [OK]
Hint: Use dot notation for nested JSON properties in assertions [OK]
Common Mistakes:
Using brackets incorrectly for nested keys
Using hyphens instead of dots for nested access
Missing quotes around string keys in bracket notation
2. Which of the following is the correct syntax to assert that response.body.data.user.age equals 30 in Postman?
easy
A. pm.test('Age is 30', () => { pm.expect(response.body.data.user.age).to.eql(30); });
B. pm.test('Age is 30', () => { pm.expect(response.body.data.user.age).to.equal('30'); });
C. pm.test('Age is 30', () => { pm.expect(response.body.data.user.age).to.be('30'); });
D. pm.test('Age is 30', () => { pm.expect(response.body.data.user.age).equals(30); });
Solution
Step 1: Identify correct assertion method
Postman uses Chai assertion library where to.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); }); uses to.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 A
Quick Check:
Use to.eql for value equality in Postman tests [OK]
Hint: Use to.eql() for exact value matching in Postman assertions [OK]
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?