[英]How to create an array from another array where certain keys matches a value in Dataweave 2.0?
[英]How to get all values where a certain Key matches in Dataweave 2.0?
有效载荷:
[
{
"Contacts": "123456,098765",
"Emails" : ""
},
{
"Contacts": "ABC123",
"Emails" : ""
}
]
如何从以下对象数组中获取所有电子邮件的列表,其中联系人 ID 从有效负载中的每一行匹配? (下面预计output)
变量 accConts
{
"queryResponse": [
{
"Email": "test123@test.com",
"SalesforceId": "123456"
},
{
"Email": "test@test.com",
"SalesforceId": "098765"
},
{
"Email": "ABC@test.com",
"SalesforceId": "ABC123"
}
]
}
预期 Output:
[
{
"Contacts": "123456,098765",
"Emails" : "test123@test.com, test@test.com"
},
{
"Contacts": "ABC123",
"Emails" : "ABC@test.com"
}
]
HTH..
%dw 2.0
output application/json
var qResp ={
"queryResponse": [
{
"Email": "test123@test.com",
"SalesforceId": "123456"
},
{
"Email": "test@test.com",
"SalesforceId": "098765"
},
{
"Email": "ABC@test.com",
"SalesforceId": "ABC123"
}
]
}
---
payload filter ($.Contacts != null) map using (iter = $$) {
"Contacts" : $.Contacts,
"Emails": (qResp.queryResponse filter (payload[iter].Contacts contains $.SalesforceId)) reduce ((item,acc = "") -> (acc ++ "," ++ item.Email)[1 to -1]
)
}
我接受了 Salim Khan 的回答,因为他引导我朝着正确的方向前进,并且让电子邮件的逻辑发挥作用。 我只需要返工 map 逻辑,
payload map (row, index) -> {
"Contacts" : row."Contacts",
"Emails" : (qResp.queryResponse filter (row."Contacts" contains $.SalesforceId)) reduce ((item,acc = "") -> (acc ++ "," ++ item.Email)[1 to -1]
),
}
想添加一个更简洁的解决方案来展示另一种方法。
%dw 2.0
output application/json
var qResp =
{
"queryResponse": [
{
"Email": "test123@test.com",
"SalesforceId": "123456"
},
{
"Email": "test@test.com",
"SalesforceId": "098765"
},
{
"Email": "ABC@test.com",
"SalesforceId": "ABC123"
}
]
}
---
payload map (value) ->
{
'Contacts':value.Contacts,
'Emails': qResp.queryResponse[?(value.Contacts contains $.SalesforceId)]..Email joinBy ", "
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.