简体   繁体   English

如何获取 Dataweave 2.0 中某个键匹配的所有值?

[英]How to get all values where a certain Key matches in Dataweave 2.0?

Payload:有效载荷:

[
{
    "Contacts": "123456,098765",
    "Emails" : ""
},
{
    "Contacts": "ABC123",
    "Emails" : ""
}
]

How can I get a list of all emails from the below array of objects where the contact Id matches from each row in the payload?如何从以下对象数组中获取所有电子邮件的列表,其中联系人 ID 从有效负载中的每一行匹配? (Expected output below) (下面预计output)

Variable accConts变量 accConts

{
    "queryResponse": [
        {
            "Email": "test123@test.com",
            "SalesforceId": "123456"
        },
        {
            "Email": "test@test.com",
            "SalesforceId": "098765"
            
        },
        {
            "Email": "ABC@test.com",
            "SalesforceId": "ABC123"
            
        }

    ]
}

Expected Output:预期 Output:

[
{
    "Contacts": "123456,098765",
    "Emails" : "test123@test.com, test@test.com"
},
{
    "Contacts": "ABC123",
    "Emails" : "ABC@test.com"
}
]

HTH.. 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]
    )

}


I accepted Salim Khan's answer as he guided me in the right direction and the logic to get emails worked.我接受了 Salim Khan 的回答,因为他引导我朝着正确的方向前进,并且让电子邮件的逻辑发挥作用。 I just needed to rework the map logic,我只需要返工 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]
    ),
}

Hopefully this comaprision helps希望这个comaprision有所帮助

工作正常

结果出错

Wanted to add a slightly more succinct solution to show another approach.想添加一个更简洁的解决方案来展示另一种方法。

%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.

相关问题 如何从另一个数组创建一个数组,其中某些键与 Dataweave 2.0 中的值匹配? - How to create an array from another array where certain keys matches a value in Dataweave 2.0? Dataweave:如何连接 json 数组中某个键的值 - Dataweave: How to concatenate values for a certain key in json array 如何从dataweave 2.0中的地图列表中获取字符串值? - how to get string values from list of maps in dataweave 2.0? Dataweave:如何过滤和连接 json 数组中某些键的值 - Dataweave: How to filter and concatenate values for certain keys in json array 如何删除在dataweave中具有null值的所有键的对象? - how to remove objects that have all keys with null values in dataweave? DataWeave 2.0 将时间差作为一个周期 - DataWeave 2.0 get time difference as a period 如何使用 dataweave 2.0 根据 xpr 中的逻辑更新 DTOSteps 节点值 - How to update the DTOSteps node values based on the logic in xpr using dataweave 2.0 如何在 JSON 中添加值以使用 Dataweave 获得累积总数 - How add values in JSON to get a cumulative total using Dataweave 如何在 dataweave 2.0 中尝试使用 function 和 map - How to use try function with map in dataweave 2.0 如何在dataweave 2.0,mule中将具有共同值的多个json对象分离到一个特定的object中? - How to segregate multiple json objects with common values into one particular object in dataweave 2.0, mule?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM