I am working my way through the DataWeave tutorial (highly recommended!) at dwlang.fun and I am finally stumped by the pluck section of the working with objects chapter. The input is a single array of four order lines that I need to group by order id.
The input is
[
{
"orderId" : 1,
"customer" : "Josh",
"lineId" : 1,
"lineItem" : "Shoes",
"price" : 50
},
{
"orderId" : 1,
"customer" : "Josh",
"lineId" : 2,
"lineItem" : "Socks",
"price" : 20
},
{
"orderId" : 2,
"customer" : "Mariano",
"lineId" : 3,
"lineItem" : "Shirt",
"price" : 30
},
{
"orderId" : 2,
"customer" : "Mariano",
"lineId" : 4,
"lineItem" : "Jacket",
"price" : 80
}
]
and the desired output is a single array containing each order in its own array:
[
[
{
"orderId": 1,
"customer": "Josh",
"lineId": 1,
"lineItem": "Shoes",
"price": 50
},
{
"orderId": 1,
"customer": "Josh",
"lineId": 2,
"lineItem": "Socks",
"price": 20
}
],
[
{
"orderId": 2,
"customer": "Mariano",
"lineId": 3,
"lineItem": "Shirt",
"price": 30
},
{
"orderId": 2,
"customer": "Mariano",
"lineId": 4,
"lineItem": "Jacket",
"price": 80
}
]
]
My code is close, but the grouping is off. My code is
%dw 2.0
output json
var myData = payload
map (order, index) -> {
(order pluck (v, k, idx) -> {
(k): v
})
}
---
myData groupBy ((order, index) -> order.orderId)
and my output is:
{
"1": [
{
"orderId": 1,
"customer": "Josh",
"lineId": 1,
"lineItem": "Shoes",
"price": 50
},
{
"orderId": 1,
"customer": "Josh",
"lineId": 2,
"lineItem": "Socks",
"price": 20
}
],
"2": [
{
"orderId": 2,
"customer": "Mariano",
"lineId": 3,
"lineItem": "Shirt",
"price": 30
},
{
"orderId": 2,
"customer": "Mariano",
"lineId": 4,
"lineItem": "Jacket",
"price": 80
}
]
}
question from:
https://stackoverflow.com/questions/65904987/where-do-i-place-the-groupby-when-using-pluck