Bird
Raised Fist0
Postmantesting~20 mins

Response body array assertions in Postman - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Response Array Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate
2:00remaining
Check if response array contains an object with a specific property value
Given a JSON response body containing an array of user objects, which Postman test script correctly asserts that at least one user has the username 'alice'?
Postman
pm.test('Response has user with username alice', () => {
    const jsonData = pm.response.json();
    // Write assertion here
});
Apm.expect(jsonData.includes('alice')).to.be.true;
Bpm.expect(jsonData.some(user => user.username === 'alice')).to.be.true;
Cpm.expect(jsonData.filter(user => user.username === 'alice')).to.have.lengthOf(0);
Dpm.expect(jsonData.find(user => user.username === 'alice')).to.equal(true);
Attempts:
2 left
💡 Hint
Use an array method that returns true if any element matches the condition.
❓ assertion
intermediate
2:00remaining
Assert the length of an array in response body
How do you write a Postman test to assert that the response body array has exactly 5 items?
Postman
pm.test('Response array length is 5', () => {
    const jsonData = pm.response.json();
    // Write assertion here
});
Apm.expect(jsonData.length).to.equal(5);
Bpm.expect(jsonData).to.have.length(5);
Cpm.expect(jsonData.size).to.equal(5);
Dpm.expect(jsonData.count).to.equal(5);
Attempts:
2 left
💡 Hint
Use the correct property to get array length in JavaScript.
❓ Predict Output
advanced
2:00remaining
What is the result of this Postman test script?
Consider the following response body and test script. What will be the test result (pass or fail)?
Postman
/* Response body JSON */
[
  {"id": 1, "active": true},
  {"id": 2, "active": false},
  {"id": 3, "active": true}
]

/* Test script */
pm.test('All users are active', () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData.every(user => user.active)).to.be.true;
});
AFail - response is not an array
BSyntaxError - incorrect use of every method
CPass - all users have active true
DFail - not all users have active true
Attempts:
2 left
💡 Hint
Check if every user object has active set to true.
❓ locator
advanced
2:00remaining
Choose the correct JSON path to select all 'name' fields in an array response
Given a response body array of objects with a 'name' property, which JSON path expression correctly selects all 'name' values?
A$[*].name
B$.name
C$.name[*]
D$[*].['name']
Attempts:
2 left
💡 Hint
Use the wildcard to select all elements in the array, then access the 'name' property.
❓ framework
expert
3:00remaining
Identify the correct Postman test script to assert sorted order of array by 'score'
You receive a response body array of objects with a numeric 'score' property. Which Postman test script correctly asserts that the array is sorted in ascending order by 'score'?
Postman
pm.test('Array is sorted by score ascending', () => {
    const jsonData = pm.response.json();
    // Write assertion here
});
Apm.expect(jsonData.map(v => v.score).sort()).to.eql(jsonData.map(v => v.score));
Bpm.expect(jsonData.reduce((acc,v) => acc && v.score >= acc.score, jsonData[0])).to.be.true;
Cpm.expect(jsonData.every((v,i,a) => i === 0 || a[i-1].score <= v.score)).to.be.true;
Dpm.expect(jsonData.sort((a,b) => a.score - b.score)).to.eql(jsonData);
Attempts:
2 left
💡 Hint
Check each element compared to the previous one to confirm ascending order.

Practice

(1/5)
1. What does a response body array assertion in Postman primarily check?
easy
A. If the API returns a list of items as expected
B. If the API response time is under 1 second
C. If the API headers contain authentication tokens
D. If the API URL is correctly formatted

Solution

  1. Step 1: Understand the purpose of response body array assertions

    These assertions verify the data returned by the API, especially when it should be a list or array of items.
  2. Step 2: Identify what is checked in the response body

    We check if the response contains an array, its length, and the properties of each item to ensure data correctness.
  3. Final Answer:

    If the API returns a list of items as expected -> Option A
  4. Quick Check:

    Response body array assertions check list presence [OK]
Hint: Focus on verifying the list of items in the response body [OK]
Common Mistakes:
  • Confusing response time with response content
  • Checking headers instead of body data
  • Assuming URL format is part of body assertions
2. Which of the following is the correct Postman test code to assert that the response body is an array?
easy
A. pm.test('Response is array', () => pm.expect(pm.response.json()).to.be.a('string'))
B. pm.test('Response is array', () => pm.expect(pm.response.text()).to.be.an('array'))
C. pm.test('Response is array', () => pm.expect(pm.response.json()).to.be.an('array'))
D. pm.test('Response is array', () => pm.expect(pm.response.body).to.be.an('object'))

Solution

  1. Step 1: Identify the correct method to parse JSON response

    pm.response.json() parses the response body as JSON, which is needed to check if it is an array.
  2. Step 2: Use the correct Chai assertion syntax

    to.be.an('array') correctly asserts the type is an array. Using pm.response.text() or wrong types will fail.
  3. Final Answer:

    pm.test('Response is array', () => pm.expect(pm.response.json()).to.be.an('array')) -> Option C
  4. Quick Check:

    Use pm.response.json() and to.be.an('array') [OK]
Hint: Use pm.response.json() with to.be.an('array') for array checks [OK]
Common Mistakes:
  • Using pm.response.text() instead of json()
  • Checking for 'string' instead of 'array'
  • Using pm.response.body without parsing
3. Given this response body: [{"id":1,"name":"Alice"},{"id":2,"name":"Bob"} ], what will this Postman test output?
pm.test('Check length', () => {
  const jsonData = pm.response.json();
  pm.expect(jsonData.length).to.eql(3);
});
medium
A. Test fails because length is 2
B. Test passes because length is undefined
C. Test throws syntax error
D. Test passes because length is 3

Solution

  1. Step 1: Parse the response and check array length

    The response JSON is an array with 2 objects, so jsonData.length equals 2.
  2. Step 2: Compare expected length with actual length

    The test expects length 3, but actual length is 2, so the assertion fails.
  3. Final Answer:

    Test fails because length is 2 -> Option A
  4. Quick Check:

    Array length is 2, expected 3, test fails [OK]
Hint: Count array items carefully before asserting length [OK]
Common Mistakes:
  • Assuming length is 3 without counting items
  • Expecting syntax error when code is correct
  • Confusing length property with array content
4. Identify the error in this Postman test code that checks if all items in the response array have an 'id' property:
pm.test('All items have id', () => {
  const jsonData = pm.response.json();
  jsonData.forEach(item => {
    pm.expect(item.id).to.exist;
  });
});
medium
A. pm.expect syntax is incorrect
B. Should use pm.expect(item).to.have.property('id') instead
C. forEach cannot be used on jsonData
D. No error, the test is correct

Solution

  1. Step 1: Check if jsonData is an array and forEach usage

    pm.response.json() returns an array, so forEach is valid to iterate over items.
  2. Step 2: Validate assertion syntax for property existence

    pm.expect(item.id).to.exist correctly asserts that the 'id' property exists on each item.
  3. Final Answer:

    No error, the test is correct -> Option D
  4. Quick Check:

    forEach and to.exist used correctly [OK]
Hint: Use forEach with to.exist to check property presence [OK]
Common Mistakes:
  • Thinking forEach can't be used on arrays
  • Confusing to.exist with to.have.property
  • Incorrect pm.expect syntax assumptions
5. You want to assert that the response array contains at least one item with status equal to active. Which Postman test code correctly does this?
hard
A. pm.test('Has active status', () => { const jsonData = pm.response.json(); pm.expect(jsonData.every(item => item.status === 'active')).to.be.true; });
B. pm.test('Has active status', () => { const jsonData = pm.response.json(); pm.expect(jsonData.some(item => item.status === 'active')).to.be.true; });
C. pm.test('Has active status', () => { const jsonData = pm.response.json(); pm.expect(jsonData.find(item => item.status === 'active')).to.be.undefined; });
D. pm.test('Has active status', () => { const jsonData = pm.response.json(); pm.expect(jsonData.filter(item => item.status === 'active').length).to.eql(0); });

Solution

  1. Step 1: Understand the requirement to find at least one item with status 'active'

    The method some() returns true if any item matches the condition, which fits the requirement.
  2. Step 2: Analyze each option's logic

    pm.test('Has active status', () => { const jsonData = pm.response.json(); pm.expect(jsonData.some(item => item.status === 'active')).to.be.true; }); uses some() and expects true, correctly asserting presence. pm.test('Has active status', () => { const jsonData = pm.response.json(); pm.expect(jsonData.every(item => item.status === 'active')).to.be.true; }); uses every(), which requires all items to be active. pm.test('Has active status', () => { const jsonData = pm.response.json(); pm.expect(jsonData.find(item => item.status === 'active')).to.be.undefined; }); expects find() result to be undefined, which means no active items. pm.test('Has active status', () => { const jsonData = pm.response.json(); pm.expect(jsonData.filter(item => item.status === 'active').length).to.eql(0); }); expects filter length to be 0, meaning no active items.
  3. Final Answer:

    pm.test('Has active status', () => { const jsonData = pm.response.json(); pm.expect(jsonData.some(item => item.status === 'active')).to.be.true; }); -> Option B
  4. Quick Check:

    Use some() to check at least one match [OK]
Hint: Use some() to check if any array item matches condition [OK]
Common Mistakes:
  • Using every() which requires all items to match
  • Checking for undefined instead of true
  • Asserting zero length for presence check
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