keyur shah
keyur shah

Reputation: 13

How to verify ARRAY response in POSTMAN?

When response is {product id : 123456789}

Below postman code works properly -

pm.test("Body is correct", function () {pm.response.to.have.body("{product id : 123456789}");});

But when response is an array like this [{product id : 123456789}]

Below both are not working--

pm.test("Body is correct", function () {pm.response.to.have.body("[{product id : 123456789}]");});

pm.test("Body is correct", function () {pm.response.to.have.body([{product id : 123456789}]);});

Any idea or specific code ?? Thank you in advance !!!

Upvotes: 1

Views: 1378

Answers (2)

Steven Scott
Steven Scott

Reputation: 11260

You can also check if the result is an array, and even loop through the array using basic Javascript.

pm.test('Validate Data is an array', function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData.Data).to.be.an.instanceOf(Array);
    pm.expect(jsonData.Data.length).to.be.greaterThan(0);
});

pm.test('Validate first product id to be 123456789', function () {
    const jsonData = pm.response.json();
    const rec = jsonData.Data[0];
    pm.expect(rec).to.contain('product id: 123456789');

    // If the field/payload is from an API, you can also do one of:                                                 
    // pm.expect(rec.ProductId).to.equal(123456789);    // expecting field from an API 
    // pm.expect(rec.ProductId).to.equal('123456789');  // if data type is a string      
});

Upvotes: 0

Darshit Shah
Darshit Shah

Reputation: 90

pm.test("Test Case Name", function () {

   var jsonData = pm.response.json();
   var xyz = [{product id : 123456789}];

 pm.expect(xyz).to.deep.equal(jsonData);

});

Upvotes: 2

Related Questions