</>

Technology

Postman

Difficulty

Intermediate

Interview Question

How do you write assertions for arrays and nested objects in Postman test scripts?

Answer

Array and Nested Object Assertions in Postman

Basic Array Access

JavaScript
const json = pm.response.json();

// Access array element by index
pm.test("First user ID is 1", function () {
    pm.expect(json.data[0].id).to.equal(1);
});

// Check array length
pm.test("Returns 6 users", function () {
    pm.expect(json.data).to.have.length(6);
});

// Check array is not empty
pm.test("Data is not empty", function () {
    pm.expect(json.data).to.be.an('array').that.is.not.empty;
});

Validate Every Element in Array

JavaScript
pm.test("All users have email addresses", function () {
    json.data.forEach(function(user) {
        pm.expect(user.email).to.be.a('string');
        pm.expect(user.email).to.include('@');
        pm.expect(user.id).to.be.a('number').above(0);
    });
});

Nested Object Assertions

JavaScript
// Response:
// {
//   "data": {
//     "id": 2,
//     "email": "janet@reqres.in",
//     "first_name": "Janet",
//     "avatar": "https://cdn...."
//   },
//   "support": {
//     "url": "https://reqres.in/#support-heading",
//     "text": "To keep..."
//   }
// }

pm.test("Nested: user data is correct", function () {
    pm.expect(json.data.id).to.equal(2);
    pm.expect(json.data.first_name).to.equal("Janet");
    pm.expect(json.data.email).to.include("reqres.in");
    pm.expect(json.data.avatar).to.match(/^https:\/\//);
});

pm.test("Nested: support object exists", function () {
    pm.expect(json.support).to.be.an('object');
    pm.expect(json.support.url).to.be.a('string');
    pm.expect(json.support.text).to.be.a('string').and.not.empty;
});

Find Element in Array

JavaScript
pm.test("Find user with id 3 in results", function () {
    const user = json.data.find(u => u.id === 3);
    pm.expect(user).to.not.be.undefined;
    pm.expect(user.first_name).to.be.a('string');
});

Array Contains Specific Value

JavaScript
pm.test("Roles array includes 'admin'", function () {
    const roles = json.user.roles; // ["admin", "editor"]
    pm.expect(roles).to.include('admin');
});

Deep Equal for Object

JavaScript
pm.test("User object matches expected", function () {
    pm.expect(json.data).to.deep.include({
        id: 2,
        first_name: "Janet",
        last_name: "Weaver"
    });
});

Array of Objects Validation

JavaScript
pm.test("All orders have required fields", function () {
    const orders = json.orders;
    orders.forEach(order => {
        pm.expect(order).to.have.property('id');
        pm.expect(order).to.have.property('status');
        pm.expect(order).to.have.property('total');
        pm.expect(order.total).to.be.above(0);
        pm.expect(['pending', 'completed', 'cancelled'])
            .to.include(order.status);
    });
});

Filter and Validate

JavaScript
pm.test("All active users have email verified", function () {
    const activeUsers = json.data.filter(u => u.status === 'active');
    activeUsers.forEach(u => {
        pm.expect(u.email_verified).to.equal(true);
    });
});

Follow AutomateQA

Related Topics