我们有多级数组,因此我们使用多个映射函数的转换。在这种情况下如何为字段 ID 分配增量值。
输入:
[
{
"productNo": "00011111",
"items": [
{
"color": "000000000006000060",
},
{
"color": "000000000006000061",
}
]
},
{
"productNo": "00022222",
"items": [
{
"color": "000000000006000060"
},
{
"color": "000000000006000061"
}
]
}
]
我为此尝试过的 Dataweave 代码:
%dw 2.0
output application/json
---
flatten(payload map (item, index) -> item.items map (subItem, subIndex) -> {
"ID": subIndex,
"PNR": item.productNo,
"Color": subItem.color
})
预期输出:
[
{
"ID": 1,
"PNR": 00011111,
"Color": "000000000006000060"
},
{
"ID": 2,
"PNR": 00011111,
"Color": "000000000006000061"
},
{
"ID": 3,
"PNR": 00022222,
"Color": "000000000006000060"
},
{
"ID": 4,
"PNR": 00022222,
"Color": "000000000006000061"
}
]
根据 dataweave,上面的 subIndex 将在下一次迭代中重置为 0,因此请让我知道我们可以使用什么来获取增量值。