我想基于一些嵌套属性来映射一个json,但是我似乎犯了一个愚蠢的错误。
我的输入:
[
{
"productNo": "00011111",
"items": [
{
"color": "000000000006000060",
},
{
"color": "000000000006000061",
}
]
},
{
"productNo": "00022222",
"items": [
{
"color": "000000000006000060"
},
{
"color": "000000000006000061"
}
]
}
]
我的转变:
%dw 2.2
output application/json
---
payload map ( prod , indexOfProd ) -> {
(prod.items map (prodItem, indexOfProdItem) -> {
PNR: prod.productNo,
Color: color.quantity
})
}
我的结果:
[
{
"PNR": 00011111,
"Color": "000000000006000060",
"PNR": 00011111,
"Color": "000000000006000061"
},
{
"PNR": 00022222,
"Color": "000000000006000060",
"PNR": 00022222,
"Color": "000000000006000061"
}
]
我的预期结果/我想要得到的是什么
[
{
"PNR": 00011111,
"Color": "000000000006000060"
},
{
"PNR": 00011111,
"Color": "000000000006000061"
},
{
"PNR": 00022222,
"Color": "000000000006000060"
},
{
"PNR": 00022222,
"Color": "000000000006000061"
}
]
有人暗示为什么不根据颜色变化来分离结果吗?
答案 0 :(得分:5)
您可以使用以下数据编织表达式:
%dw 2.0
output application/json
---
flatten(payload map (item, index) -> item.items map (subItem, subIndex) -> {
"PNR": item.productNo,
"Color": subItem.color
})
答案 1 :(得分:1)
使用现有解决方案Martin的另一种变体。
%dw 2.0
output application/json
---
flatten (payload map ( prod , indexOfProd ) -> {
temp: (prod.items map (prodItem, indexOfProdItem) -> {
PNR: prod.productNo,
Color: prodItem.color
})
}.temp)