1

我对 DataWeave 比较陌生,想知道如何/是否可以使用 DataWeave 而不是 Java 转换器来转换如下所示的消息。

如果我有以下用户的 JSON 有效负载,以及一个组和子组:

[{
    "GROUP": "GROUP_A",
    "SUBGROUP": "SUBGROUP A1",
    "USER": "USER 1"
}, {
    "GROUP": "GROUP_B",
    "SUBGROUP": "SUBGROUP B1",
    "USER": "USER 1"
}, {
    "GROUP": "GROUP_B",
    "SUBGROUP": "SUBGROUP B1",
    "USER": "USER 2"
}, {
    "GROUP": "GROUP_B",
    "SUBGROUP": "SUBGROUP B2",
    "USER": "USER 3"
}, {
    "GROUP": "GROUP_B",
    "SUBGROUP": "SUBGROUP B2",
    "USER": "USER 4"
}, {
    "GROUP": "GROUP_B",
    "SUBGROUP": "SUBGROUP B2",
    "USER": "USER 5"
}]

将有效负载转换为如下结构的 DataWeave 转换会是什么样子:

[   
    {
        "GROUP": "GROUP_A",
        "SUBGROUPS": [{
            "NAME": "SUBGROUP A1",
            "USERS": ["USER 1"]
        }]
    }, {
        "GROUP": "GROUP_B",
        "SUBGROUPS": [{
            "NAME": "SUBGROUP B1",
            "USERS": ["USER 1", "USER 2"]
        }, {
            "NAME": "SUBGROUP B2",
            "USERS": ["USER 3", "USER 4", "USER 5"]
        }]
    }

]

谢谢你的帮助!

4

1 回答 1

4

DataWeave 开发请参考DataWeave 参考文档。在这种情况下,您可以参考 Group by ... 部分。要转换上述消息,请尝试以下脚本:

%dw 1.0
%output application/json
---
payload groupBy $.GROUP pluck {
    GROUP: $$,
    SUBGROUPS: $ groupBy $.SUBGROUP pluck {
        NAME: $$,
        USERS: $.USER
    }
}
于 2016-02-10T06:33:28.520 回答