[英]Dataweave 2.0 reduce function to reduce the array of json Mule
My Input Json Looks like below:-我的输入 Json 如下所示:-
[{
"eId": "123",
"eType": "NZ",
"value": [{
"tId": "444"
}, {
"tId": "555"
}
]
}, {
"eId": "456",
"eType": "AU",
"value": [{
"tId": "666"
}
]
}
]
Expected Output Json to be like:预计 Output Json 会像:
[{
"eId": "123",
"eType": "NZ",
"tId": "444"
}, {
"eId": "123",
"eType": "NZ",
"tId": "555"
}, {
"eId": "456",
"eType": "AU",
"tId": "666"
}
]
I tried Transform using the reduce function like below.我尝试使用如下所示的 reduce function 进行变换。 I am not getting expected response
我没有得到预期的回应
%dw 2.0
output application/json
---
payload reduce ((val, acc = []) ->
acc + {
"id" : val.eId, "type": val.eType, "Tid": val.value map $.tId
})
Could someone please correct me where I am doing it wrong有人可以纠正我在哪里做错了吗
I'll make an educated guess, because the example expected output is incomplete, that the expected output should look like:我会做出有根据的猜测,因为预期的示例 output 不完整,预期的 output 应该如下所示:
[
{
"eId": "123",
"type": "Co",
"tId": "444"
},
{
"eId": "123",
"type": "Co",
"tId": "555"
}
]
Script:脚本:
%dw 2.0
output application/json
fun mapItem(i) = i.value map { eId: i.eId, "type": i.eType, tId: $.tId }
---
payload flatMap mapItem($)
Feel free to edit the question to add more details if needed.如果需要,请随时编辑问题以添加更多详细信息。
You can try using the following dataweave expression:您可以尝试使用以下 dataweave 表达式:
%dw 2.0
output application/json
---
flatten(payload map (it1, ix1) ->
it1.value map (it2, ix2) -> {
eId: it1.eId,
eType: it1.eType,
tId: it2.tId
})
Try with this.试试这个。
%dw 2.0
output application/json
var inp = payload
---
payload[0].value map {
eId: inp[0].eId,
Tid: $.tId
}
Updated solution更新的解决方案
%dw 2.0
output application/json
---
payload flatMap ((item, index) -> {
a: item.value map {
eId:item.eId,
eType: item.eType,
tId: $.tId
}}.a)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.