How to get a desired output using groupBy in dataweave?

Viewed 53

I'm looking for an output similar to this one below where i want to groupBy costomer and orderid.

Input:

[ { "item": 621, "orderid": "ON22", "qty": 45.0, "customer": "610", "date": "1988-08-13" }, { "item": 63, "orderid": "ON2234", "qty": 7, "customer": "813", "date": "2001-08-13" } ]

Desired output: [ { "customer":"813", "data":[ { "item":63, "qty":7, "orderid":"ON2234", "date":"2001-08-13" } ] }, { "customer":"610", "data":[ { "item": 621, "qty": 45.0, "orderid": "ON22", "date": "1988-08-13" } ] } ]

1 Answers

You can simply map the default output of your groupBy result since your output does not require any additional logic.

%dw 2.0
output application/json
---
payload groupBy $.customer pluck ((customerOrders, customerId) -> {
    customer: customerId as String,
    data: customerOrders
})
Related