Challenge - 5 Problems
JSON Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate2: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"
}
}Attempts:
2 left
💡 Hint
Look carefully at the JSON structure and the expected value in the assertion.
✗ Incorrect
Option A correctly accesses the nested 'status' key inside 'user' and checks if it equals 'active'. Option A checks for 'inactive' which is wrong. Option A tries to access 'status' at the root level, which does not exist. Option A incorrectly expects a boolean value.
❓ assertion
intermediate2: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"]
}Attempts:
2 left
💡 Hint
Check the length property and the expected number type.
✗ Incorrect
Option B correctly checks that the length of the 'items' array is 3. Option B checks for length 4 which is incorrect. Option B compares length to string '3' instead of number 3, causing a failed assertion. Option B wrongly expects 'items' to be an object instead of an array.
❓ Predict Output
advanced2: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'); });
Attempts:
2 left
💡 Hint
Look at the JSON response structure and the value at index 1 in the 'tags' array.
✗ Incorrect
The JSON response has 'tags' as ["normal", "urgent", "review"]. The second element (index 1) is 'urgent', so the assertion passes.
🔧 Debug
advanced2: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); });
Attempts:
2 left
💡 Hint
Check if the JSON response contains the 'price' key.
✗ Incorrect
If the JSON response does not have a 'price' key, accessing pm.response.json().price returns undefined. Then the assertion tries to compare undefined to 19.99, causing a TypeError.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Consider how to avoid errors when the key is missing.
✗ Incorrect
Option C safely checks if 'discount' exists before asserting its value. Option C fails if 'discount' is missing (undefined). Option C tries to chain 'to.exist' and 'to.be.above(0)' which causes an error if 'discount' is undefined. Option C fails if 'discount' is 0 because 0 is falsy, so the check is skipped incorrectly.