如何在dataweave中对flatboject数组进行分组
how to groupBy flatboject array in dataweave
我有下面的数组 json,它来自阅读 text/csv 文件。例如
[{
"no" : "2001",
"sendingStoreCode": "006091",
"receivingStoreCode": "006095",
"sku": "dddd",
"quantity": 1
},
{
"no" : "2001",
"sendingStoreCode": "006091",
"receivingStoreCode": "006095",
"sku": "xxxx",
"quantity": 1
},
{
"no" : "2002",
"sendingStoreCode": "006091",
"receivingStoreCode": "006095",
"sku": "xxxx",
"quantity": 1
}
]
现在我们必须通过 groupBy #no、sendingStoreCode 等来合并数据行,
下面是预期的结果,我可以知道如何得到它吗
[{
"no": "2001",
"sendingStoreCode": "006091",
"receivingStoreCode": "006095",
"deliveryItems": [{
"line": 1,
"sku": "dddd",
"quantity": 1
},
{
"line": 2,
"sku": "xxxx",
"quantity": 1
}
]
}, {
"no": "2002",
"sendingStoreCode": "006091",
"receivingStoreCode": "006095",
"deliveryItems": [{
"sku": "xxxx",
"quantity": 1
}]
}]
也许你可以尝试这样的事情,注意在 groupBy
函数中,你必须创建一个用作 id
的单个值
%dw 2.0
output application/json
---
payload
groupBy ((item, index) -> item.no ++ "-" ++ item.sendingStoreCode ++ "-" ++ item.receivingStoreCode)
pluck ((value, key, index) -> {
no: value.no[0],
sendingStoreCode: value.sendingStoreCode[0],
receivingStoreCode: value.receivingStoreCode[0],
deliveryItems: value map ((item, index) -> {
"line" : index + 1,
"sku": item.sku,
"quantity": item.quantity
})
})
%dw 2.0
output application/json
---
payload groupBy ($.no++"_"++$.sendingStoreCode) mapObject {
"no": $.no[0],
"sendingStoreCode" : $.sendingStoreCode[0],
"receivingStoreCode" : $.receivingStoreCode[0],
deliveryitems: $ map {
line: ($$+1),
sku: $.sku,
quantity: $.quantity
}
}
我有下面的数组 json,它来自阅读 text/csv 文件。例如
[{
"no" : "2001",
"sendingStoreCode": "006091",
"receivingStoreCode": "006095",
"sku": "dddd",
"quantity": 1
},
{
"no" : "2001",
"sendingStoreCode": "006091",
"receivingStoreCode": "006095",
"sku": "xxxx",
"quantity": 1
},
{
"no" : "2002",
"sendingStoreCode": "006091",
"receivingStoreCode": "006095",
"sku": "xxxx",
"quantity": 1
}
]
现在我们必须通过 groupBy #no、sendingStoreCode 等来合并数据行, 下面是预期的结果,我可以知道如何得到它吗
[{
"no": "2001",
"sendingStoreCode": "006091",
"receivingStoreCode": "006095",
"deliveryItems": [{
"line": 1,
"sku": "dddd",
"quantity": 1
},
{
"line": 2,
"sku": "xxxx",
"quantity": 1
}
]
}, {
"no": "2002",
"sendingStoreCode": "006091",
"receivingStoreCode": "006095",
"deliveryItems": [{
"sku": "xxxx",
"quantity": 1
}]
}]
也许你可以尝试这样的事情,注意在 groupBy
函数中,你必须创建一个用作 id
%dw 2.0
output application/json
---
payload
groupBy ((item, index) -> item.no ++ "-" ++ item.sendingStoreCode ++ "-" ++ item.receivingStoreCode)
pluck ((value, key, index) -> {
no: value.no[0],
sendingStoreCode: value.sendingStoreCode[0],
receivingStoreCode: value.receivingStoreCode[0],
deliveryItems: value map ((item, index) -> {
"line" : index + 1,
"sku": item.sku,
"quantity": item.quantity
})
})
%dw 2.0
output application/json
---
payload groupBy ($.no++"_"++$.sendingStoreCode) mapObject {
"no": $.no[0],
"sendingStoreCode" : $.sendingStoreCode[0],
"receivingStoreCode" : $.receivingStoreCode[0],
deliveryitems: $ map {
line: ($$+1),
sku: $.sku,
quantity: $.quantity
}
}