0
0
Postmantesting~5 mins

JSON value assertions in Postman

Choose your learning style9 modes available
Introduction

JSON value assertions help check if the data returned by an API is correct. They make sure the API works as expected.

When testing if an API returns the right user name after login.
When verifying that a product price in the response matches the expected value.
When checking if a status field in the JSON response shows 'success' after an operation.
When confirming that a list of items in the response contains the expected number of elements.
Syntax
Postman
pm.test("Test description", function () {
    pm.expect(pm.response.json().key).to.eql(expectedValue);
});

Use pm.response.json() to get the JSON response body.

pm.expect() is used to write assertions on values.

Examples
This checks if the user name in the JSON response is exactly "Alice".
Postman
pm.test("Check user name", function () {
    pm.expect(pm.response.json().user.name).to.eql("Alice");
});
This confirms the status field equals "success".
Postman
pm.test("Verify status is success", function () {
    pm.expect(pm.response.json().status).to.eql("success");
});
This asserts the product price is 19.99.
Postman
pm.test("Check product price", function () {
    pm.expect(pm.response.json().product.price).to.eql(19.99);
});
Sample Program

This test checks three things: the user ID is 101, the user is active, and the user has the role "admin".

Postman
pm.test("Validate JSON response values", function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData.user.id).to.eql(101);
    pm.expect(jsonData.user.active).to.eql(true);
    pm.expect(jsonData.user.roles).to.include("admin");
});
OutputSuccess
Important Notes

Always check the exact path to the JSON value you want to assert.

Use to.eql() for exact matches and to.include() to check if an array contains a value.

Run tests in Postman's Test tab to see results clearly.

Summary

JSON value assertions verify API response data is correct.

Use pm.expect() with pm.response.json() to write assertions.

Assertions help catch errors early and keep APIs reliable.