Bird
Raised Fist0
Postmantesting~20 mins

Nested object assertions in Postman - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate
2:00remaining
Check nested property value in JSON response
Given the following JSON response, which Postman test assertion correctly verifies that the user's city is 'New York'?
Postman
{
  "user": {
    "id": 123,
    "name": "Alice",
    "address": {
      "city": "New York",
      "zip": "10001"
    }
  }
}
Apm.test('City is New York', () => { pm.expect(pm.response.json().user.city).to.eql('New York'); });
Bpm.test('City is New York', () => { pm.expect(pm.response.json().user.address.city).to.eql('New York'); });
Cpm.test('City is New York', () => { pm.expect(pm.response.json().address.city).to.eql('New York'); });
Dpm.test('City is New York', () => { pm.expect(pm.response.json().user.address.zip).to.eql('New York'); });
Attempts:
2 left
💡 Hint
Remember to follow the exact path to the nested property in the JSON structure.
❓ assertion
intermediate
2:00remaining
Verify array length inside nested object
You receive this JSON response. Which assertion correctly checks that the user has exactly 3 phone numbers?
Postman
{
  "user": {
    "phones": ["123-4567", "234-5678", "345-6789"]
  }
}
Apm.test('User has 3 phones', () => { pm.expect(pm.response.json().user.phones).to.have.lengthOf(3); });
Bpm.test('User has 3 phones', () => { pm.expect(pm.response.json().phones.length).to.eql(3); });
Cpm.test('User has 3 phones', () => { pm.expect(pm.response.json().user.phones.length).to.eql(3); });
Dpm.test('User has 3 phones', () => { pm.expect(pm.response.json().user.phones).to.eql(3); });
Attempts:
2 left
💡 Hint
Use the correct Chai assertion to check array length.
❓ Predict Output
advanced
1:30remaining
Output of nested object assertion with missing property
What will be the result of this Postman test if the JSON response is missing the 'email' property inside 'user'?
Postman
pm.test('User email exists', () => {
  pm.expect(pm.response.json().user.email).to.exist;
});
ATest is skipped automatically
BTest passes because 'email' is optional
CTest throws a runtime error due to accessing undefined property
DTest fails with assertion error because 'email' is undefined
Attempts:
2 left
💡 Hint
Check how Chai's 'to.exist' behaves with undefined values.
🔧 Debug
advanced
2:00remaining
Identify error in nested object assertion code
This Postman test is intended to check if the user's first hobby is 'reading'. What is the error in the code?
Postman
pm.test('First hobby is reading', () => {
  pm.expect(pm.response.json().user.hobbies[0] === 'reading');
});
AMissing assertion method like 'to.eql' causes test to always pass
BIncorrect property path to hobbies array
CUsing '===' inside pm.expect causes syntax error
DNo error, test is correct
Attempts:
2 left
💡 Hint
pm.expect needs a matcher method to assert values.
❓ framework
expert
3:00remaining
Best practice for nested object assertions in Postman tests
Which approach is best to safely assert a deeply nested property 'user.profile.contact.email' exists and equals 'test@example.com' without causing runtime errors if intermediate properties are missing?
Apm.expect(pm.response.json().user.profile.contact.email).to.eql('test@example.com');
Bif(pm.response.json().user && pm.response.json().user.profile && pm.response.json().user.profile.contact) { pm.expect(pm.response.json().user.profile.contact.email).to.eql('test@example.com'); }
Cconst email = _.get(pm.response.json(), 'user.profile.contact.email'); pm.expect(email).to.eql('test@example.com');
Dpm.expect(pm.response.json()?.user?.profile?.contact?.email).to.eql('test@example.com');
Attempts:
2 left
💡 Hint
Consider safe access methods to avoid runtime errors in nested assertions.

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

  1. Step 1: Understand JSON nested property access

    In JavaScript, nested properties are accessed using dot notation like object.property.
  2. Step 2: Apply correct syntax in Postman test

    To assert user.name, use pm.expect(jsonData.user.name) which correctly accesses the nested value.
  3. Final Answer:

    Use pm.expect(jsonData.user.name) to assert the nested value. -> Option B
  4. 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

  1. Step 1: Identify correct assertion method

    Postman uses Chai assertion library where to.eql(value) checks deep equality including numbers.
  2. 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.
  3. Final Answer:

    pm.test('Age is 30', () => { pm.expect(response.body.data.user.age).to.eql(30); }); -> Option A
  4. Quick Check:

    Use to.eql for value equality in Postman tests [OK]
Hint: Use to.eql() for exact value matching in Postman assertions [OK]
Common Mistakes:
  • Using string '30' instead of number 30
  • Using incorrect assertion methods like to.be()
  • Missing arrow function syntax in pm.test
3. Given the JSON response:
{"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);
});
medium
A. Test will pass because first item's qty is 3.
B. Test will fail because qty is 2 for first item.
C. Test will throw an error due to wrong index access.
D. Test will fail because items is not an array.

Solution

  1. Step 1: Analyze JSON structure and access

    The JSON has an array items with first element having qty: 3.
  2. Step 2: Check assertion correctness

    The test accesses items[0].qty and asserts it equals 3, which matches the JSON data.
  3. Final Answer:

    Test will pass because first item's qty is 3. -> Option A
  4. Quick Check:

    Array index 0 qty = 3 matches assertion [OK]
Hint: Check array index and property value carefully in nested assertions [OK]
Common Mistakes:
  • Confusing array index starting at 1 instead of 0
  • Assuming items is an object, not an array
  • Misreading qty values in nested objects
4. You wrote this Postman test:
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?
medium
A. The assertion method to.eql is incorrect for strings.
B. The city value is misspelled in the assertion.
C. The JSON response is not parsed correctly with pm.response.json().
D. The address property is missing or undefined in user.

Solution

  1. Step 1: Understand the error message

    The error says Cannot read property 'city' of undefined, meaning address is undefined.
  2. Step 2: Identify cause in nested access

    If jsonData.user.address is missing, trying to access .city causes this error.
  3. Final Answer:

    The address property is missing or undefined in user. -> Option D
  4. Quick Check:

    Undefined parent property causes TypeError in nested access [OK]
Hint: Check if all parent nested objects exist before accessing child [OK]
Common Mistakes:
  • Assuming all nested keys always exist
  • Ignoring error message details
  • Blaming assertion method instead of data structure
5. You receive this JSON response:
{"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?
hard
A. pm.test('All users active', () => { const users = pm.response.json().users; pm.expect(users[0].every(user => user.profile.active)).to.eql(true); });
B. pm.test('All users active', () => { const users = pm.response.json().users; pm.expect(users.profile.active).to.eql(true); });
C. pm.test('All users active', () => { const users = pm.response.json().users; users.forEach(user => { pm.expect(user.profile.active).to.eql(true); }); });
D. pm.test('All users active', () => { const users = pm.response.json().users; users.map(user => pm.expect(user.profile).to.eql(true)); });

Solution

  1. Step 1: Understand requirement to check all users' active status

    We must assert each user's profile.active is true.
  2. 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 each profile.active equals 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 access users.profile.active which 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 calls every() on users[0] (an object), causing TypeError as objects have no every method.
    pm.test('All users active', () => { const users = pm.response.json().users; users.map(user => pm.expect(user.profile).to.eql(true)); }); asserts user.profile equals true, but profile is an object not equal to true and misses .active.
  3. Final Answer:

    Use forEach loop to assert each user's profile.active is true. -> Option C
  4. Quick Check:

    Loop and assert each nested property for all items [OK]
Hint: Use forEach to assert nested properties on all array items [OK]
Common Mistakes:
  • Accessing nested property on array directly
  • Calling array methods like every() on single objects
  • Asserting entire object equals true instead of its boolean property
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard