Bird
Raised Fist0
Postmantesting~20 mins

JSON value assertions in Postman - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of JSON value assertions in Postman?
easy
A. To send requests to the server
B. To check if the API response data matches expected values
C. To format JSON data for display
D. To create new API endpoints

Solution

  1. Step 1: Understand JSON value assertions

    JSON value assertions verify that the data returned by an API is correct and matches what is expected.
  2. Step 2: Identify the purpose in Postman

    In Postman, assertions are used to check API responses, not to send requests or format data.
  3. Final Answer:

    To check if the API response data matches expected values -> Option B
  4. Quick Check:

    Assertions verify response data = A [OK]
Hint: Assertions check response data correctness fast [OK]
Common Mistakes:
  • Confusing assertions with sending requests
  • Thinking assertions format JSON
  • Believing assertions create APIs
2. Which of the following is the correct syntax to assert that the JSON response has a key 'status' with value 'success' in Postman?
easy
A. pm.expect(response.status).to.equal('success');
B. pm.response.json().status == 'success';
C. pm.expect(pm.response.json().status).to.eql('success');
D. assert(pm.response.status == 'success');

Solution

  1. Step 1: Identify correct assertion syntax in Postman

    Postman uses pm.expect() with the JSON response accessed by pm.response.json(). The method to check equality is .to.eql()
  2. Step 2: Eliminate incorrect options

    pm.expect(pm.response.json().status).to.eql('success'); correctly uses pm.expect(pm.response.json().status).to.eql('success'); The other options have syntax errors or incorrect usage such as wrong response reference, missing pm.expect, or invalid functions.
  3. Final Answer:

    pm.expect(pm.response.json().status).to.eql('success'); -> Option C
  4. Quick Check:

    pm.expect + pm.response.json() + .to.eql() = D [OK]
Hint: Use pm.expect with pm.response.json() and .to.eql() [OK]
Common Mistakes:
  • Using == instead of .to.eql()
  • Missing pm.expect wrapper
  • Referencing response incorrectly
3. Given the following Postman test code:
const jsonData = pm.response.json();
pm.expect(jsonData.user.id).to.eql(12345);

What will happen if the API response JSON is { "user": { "id": 12345, "name": "Alice" } }?
medium
A. The test will pass because the user id matches 12345
B. The test will fail because the user id is a number, not a string
C. The test will fail due to syntax error
D. The test will pass but ignore the user id

Solution

  1. Step 1: Analyze the JSON response

    The response JSON has user.id equal to the number 12345, which matches the expected value in the assertion.
  2. Step 2: Understand the assertion behavior

    pm.expect().to.eql() compares values strictly but 12345 number matches 12345 number, so the assertion passes.
  3. Final Answer:

    The test will pass because the user id matches 12345 -> Option A
  4. Quick Check:

    Matching values = test passes = B [OK]
Hint: Check exact value and type in JSON for assertion [OK]
Common Mistakes:
  • Assuming number vs string mismatch causes failure
  • Confusing syntax error with assertion failure
  • Ignoring actual JSON structure
4. You wrote this Postman test:
pm.expect(pm.response.json().data).to.equal({"status": "active"});

But the test always fails even when the API returns { "data": { "status": "active" } }. What is the likely problem?
medium
A. The assertion should use .to.eql() for deep equality
B. The response JSON is not parsed correctly
C. Using .to.equal() instead of .to.eql() for object comparison
D. The test is missing pm.response.json() call

Solution

  1. Step 1: Understand difference between .to.equal() and .to.eql()

    .to.equal() checks strict equality for primitives but can fail for objects or deep checks. .to.eql() is used for deep equality including strings and objects.
  2. Step 2: Identify the correct assertion for object value

    Since 'data' is an object, .to.eql() is required for deep equality checks. Using .to.equal() performs strict reference equality (===), which fails for distinct object instances even if contents match.
  3. Final Answer:

    The assertion should use .to.eql() for deep equality -> Option A
  4. Quick Check:

    Use .to.eql() for object equality in JSON assertions [OK]
Hint: Use .to.eql() for JSON value assertions, not .to.equal() [OK]
Common Mistakes:
  • Confusing .to.equal() and .to.eql()
  • Assuming JSON parsing failed
  • Missing pm.response.json() call
5. You want to assert that the API response JSON contains an array 'items' where each item has a 'price' greater than 0. Which Postman test code correctly checks this?
hard
A. pm.expect(pm.response.json().items.filter(price > 0)).to.not.be.empty;
B. pm.expect(pm.response.json().items).to.have.property('price').above(0);
C. pm.expect(pm.response.json().items.price).to.be.above(0);
D. pm.expect(pm.response.json().items.every(item => item.price > 0)).to.be.true;

Solution

  1. Step 1: Understand the requirement

    We need to check that every item in the 'items' array has a 'price' greater than 0.
  2. Step 2: Analyze each option

    pm.expect(pm.response.json().items.every(item => item.price > 0)).to.be.true; uses JavaScript's every() method to check all items satisfy price > 0, then asserts true. This is correct.
    pm.expect(pm.response.json().items).to.have.property('price').above(0); incorrectly tries to check property on array directly.
    pm.expect(pm.response.json().items.price).to.be.above(0); tries to access items.price which is invalid since items is an array.
    pm.expect(pm.response.json().items.filter(price > 0)).to.not.be.empty; uses filter incorrectly without a function.
  3. Final Answer:

    pm.expect(pm.response.json().items.every(item => item.price > 0)).to.be.true; -> Option D
  4. Quick Check:

    Use every() with pm.expect to check all array items [OK]
Hint: Use .every() to assert all array items meet condition [OK]
Common Mistakes:
  • Trying to access property on array directly
  • Using filter without a function
  • Misusing assertion methods on arrays