我想将所有项目的列表与相同的companyName
和companyId
合并。
我需要帮助处理数据,以便它会为companyName
和companyId
获取具有相同值的所有数据,并将其合并到自己的数组中。
输入示例我期望的数据将在此处:
[{
"companyName": "ABC company",
"companyId": "123",
"projectName": "project1",
"levelCode": "893",
"projectStatus": "2"
},
{
"companyName": "ABC company",
"companyId": "123",
"projectName": "project2",
"levelCode": "1386",
"userId": "183",
"projectStatus": "2"
},
{
"companyName": "ABC company",
"companyId": "123",
"projectName": "project3",
"levelCode": "1655",
"userId": "183",
"projectStatus": "1"
},
{
"companyName": "ABC company",
"companyId": "123",
"projectName": "project4",
"levelCode": "1865",
"userId": "159",
"projectStatus": "2"
}]
预期产出
操作所有数据之后:请注意,涉及companyName
“ABC公司”的所有项目现在都合并为一个数组(项目)。
[
{
"companyName": "ABC company",
"companyId": "123",
"projects": {
"projectName": "project1",
"levelCode": "893",
"projectStatus": "2"
},
{
"projectName": "project2",
"levelCode": "1386",
"userId": "183",
"projectStatus": "2"
},
{
"projectName": "project3",
"levelCode": "1655",
"userId": "183",
"projectStatus": "1"
},
{
"projectName": "project4",
"levelCode": "1865",
"userId": "159",
"projectStatus": "2"
}
}
]
答案 0 :(得分:1)
This will work:
%dw 1.0
%output application/json
---
payload distinctBy $.companyName map ((result, IndexOfResult) -> {
companyName: result.companyName,
companyId: result.companyId,
project: payload[?($.companyName == result.companyName)] map {
projectName: $.projectName,
levelCode: $.levelCode,
userId: $.userId,
projectStatus: $.projectStatus
}
})
答案 1 :(得分:1)
检查出来。
%dw 1.0
%output application/json
---
payload distinctBy $.CompanyName map {
companyName: $.companyName,
companyID: $.companyId,
Projects: payload map {
projectName: $.projectName,
levelCode: $.levelCode,
userId: $.userId,
projectStatus: $.projectStatus
}
}
答案 2 :(得分:0)
您可以使用GroupBy运算符轻松完成此操作。 在这里,您需要按companyName和companyId进行分组 payload groupBy $ .companyName,companyId
请阅读以下链接,了解有关groupby运营商的详细信息。 https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators#group-by