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);
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With