使用Dataweave在表列中拆分连接的值

问题描述 投票:0回答:1

我在Salesforce表中有示例数据,如下所示:

[1]: https://i.stack.imgur.com/f6Y9R.png

我使用的是Mule 3.9,正在运行dataweave 1.0。我需要使用dataweave读取上述数据(从Salesforce表中)并将其转换为JSON,如下所示:

[
      {"Id": "634594cc","Name": "Alpha","List": "AB01"},
      {"Id": "634594cc","Name": "Alpha","List": "AB02"},
      {"Id": "634594cc","Name": "Alpha","List": "AB03"},
      {"Id": "5d839e9c","Name": "Bravo","List": "CD01"},
      {"Id": "5d839e9c","Name": "Bravo","List": "CD02"},
      {"Id": "3a5f34d3","Name": "Charlie","List": null}
] 

如您在上面看到的,“ List”列是我需要在最终JSON中拆分为单独数组的内容。它在开头,中间和结尾都有分号的数据。

感谢您的帮助。

mule mule-component anypoint-studio dataweave mulesoft
1个回答
0
投票

我随意创建了一些基于SS的示例数据(BTW,最好不要使用SS:)。

尝试一下:

%dw 1.0
%output application/dw
%var data = [
    {
        id: "ABC123",
        name: "A",
        list: ";AB1;AB2;AB3;"
    },
    {
        id: "ZXY321",
        name: "B",
        list: null
    }
]
---
data reduce (e,result=[]) -> (
    result ++ using (
        list = e.list default "" splitBy /;/ filter ($ != ""),
        sharedFields = {
            Id: e.id,
            Name: e.name
        }
    ) (
        using (
            flist = list when ((sizeOf list) > 0) otherwise [null]
        ) (
            flist map {
                (sharedFields),
                List: $
            }
        )

    )
)
© www.soinside.com 2019 - 2024. All rights reserved.