Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
173
rated 0 times [  180] [ 7]  / answers: 1 / hits: 20577  / 6 Years ago, thu, may 17, 2018, 12:00:00

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);

More From » postman

 Answers
68

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


[#54414] Sunday, May 13, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
byrondonavanc

Total Points: 675
Total Questions: 107
Total Answers: 105

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
;