Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman Tests Console Log

I'm trying to write up a console.log under Tests in Postman to do two things

1- Count how many types/Unique "ShipmentStatus" were received in the response body (In-Process,Shipped, & Rejected)

2 - How how many of each ShipmentStatus we received for example (4 Orders In-Process 5 Orders in Complete)

How can I accomplish 1 and 2 to show up in my console.log?

"Orders": [
    {
        "id": 544789,
        "Company": {
            "ClientName": "Therese",
        },
        "Shipping": {
            "Address": "Street,",
        },

        "userId": "1413260",
        "ShipmentStatus": "In-Process"
    },
    {
        "id": 544787,
        "Company": {
            "ClientName": "Therese",
        },
        "Shipping": {
            "Address": "Street,",
        },

        "userId": "1413260",
        "ShipmentStatus": "Shipped"
    },
            {
        "id": 544786,
        "Company": {
            "ClientName": "Therese",
        },
        "Shipping": {
            "Address": "Street,",
        },

        "userId": "1413260",
        "ShipmentStatus": "Rejected"
    },

I'm able to obtain the total records received by doing the following

 console.log("Total Orders : " + response.Orders.length);
like image 207
rookievmc Avatar asked Sep 06 '25 02:09

rookievmc


1 Answers

You could do something very quick and dirty like this, if all you want to see is those values logged out to the Console:

console.log(`Total Orders: ${pm.response.json().Orders.length}`)

var counts = {};
for (var i = 0; i < pm.response.json().Orders.length; i++) {
    counts[pm.response.json().Orders[i].ShipmentStatus] = 1 + (counts[pm.response.json().Orders[i].ShipmentStatus] || 0);
}

console.log(counts)

I extended your JSON response so that you could see what it would look like if you had more items:

Postman Console

like image 131
Danny Dainton Avatar answered Sep 07 '25 16:09

Danny Dainton