0
0
Postmantesting~20 mins

JSON value assertions in Postman - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2:00remaining
Check if JSON response contains a specific key with expected value
Given the JSON response below, which Postman test script assertion correctly verifies that the user's status is 'active'?
Postman
{
  "user": {
    "id": 101,
    "name": "Alice",
    "status": "active"
  }
}
Apm.test('User status is active', () => { pm.expect(pm.response.json().user.status).to.eql('active'); });
Bpm.test('User status is active', () => { pm.expect(pm.response.json().user.status).to.equal('inactive'); });
Cpm.test('User status is active', () => { pm.expect(pm.response.json().status).to.eql('active'); });
Dpm.test('User status is active', () => { pm.expect(pm.response.json().user.status).to.be.true; });
Attempts:
2 left
💡 Hint
Look carefully at the JSON structure and the expected value in the assertion.
assertion
intermediate
2:00remaining
Verify array length in JSON response
Which Postman test script assertion correctly verifies that the 'items' array in the JSON response has exactly 3 elements?
Postman
{
  "items": ["apple", "banana", "cherry"]
}
Apm.test('Items array length is 3', () => { pm.expect(pm.response.json().items).to.have.lengthOf(4); });
Bpm.test('Items array length is 3', () => { pm.expect(pm.response.json().items.length).to.eql(3); });
Cpm.test('Items array length is 3', () => { pm.expect(pm.response.json().items.length).to.equal('3'); });
Dpm.test('Items array length is 3', () => { pm.expect(pm.response.json().items).to.be.an('object'); });
Attempts:
2 left
💡 Hint
Check the length property and the expected number type.
Predict Output
advanced
2:00remaining
Output of JSON value assertion with nested arrays
What will be the result of this Postman test script when run against the given JSON response?
Postman
const jsonData = pm.response.json();
pm.test('Check second tag is "urgent"', () => {
  pm.expect(jsonData.tags[1]).to.eql('urgent');
});
ATest throws an error because 'tags' is not an array.
BTest fails because the second tag is 'important'.
CTest fails because the second tag is undefined.
DTest passes because the second tag is 'urgent'.
Attempts:
2 left
💡 Hint
Look at the JSON response structure and the value at index 1 in the 'tags' array.
🔧 Debug
advanced
2:00remaining
Identify the error in JSON value assertion
This Postman test script is intended to check if the 'price' in the JSON response is 19.99. What error will occur when running this script?
Postman
pm.test('Price is 19.99', () => {
  pm.expect(pm.response.json().price).to.equal(19.99);
});
ATypeError because 'price' is undefined in the JSON response.
BAssertionError because the price is 20.00, not 19.99.
CSyntaxError due to missing parentheses.
DNo error; test passes successfully.
Attempts:
2 left
💡 Hint
Check if the JSON response contains the 'price' key.
🧠 Conceptual
expert
3:00remaining
Best practice for asserting optional JSON keys in Postman
In a JSON response, some keys may or may not be present. Which Postman test script snippet correctly asserts that if the key 'discount' exists, its value must be greater than 0, but does not fail if 'discount' is missing?
Apm.test('Discount check', () => { if (pm.response.json().discount) { pm.expect(pm.response.json().discount).to.be.above(0); } });
Bpm.test('Discount check', () => { pm.expect(pm.response.json().discount).to.be.above(0); });
Cpm.test('Discount check', () => { const d = pm.response.json().discount; if (d !== undefined) { pm.expect(d).to.be.above(0); } });
Dpm.test('Discount check', () => { pm.expect(pm.response.json().discount).to.exist.and.to.be.above(0); });
Attempts:
2 left
💡 Hint
Consider how to avoid errors when the key is missing.