0

我想将所有项目的列表与相同的companyNamecompanyId.

我需要帮助处理数据,以便它将具有相同值的所有数据用于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"
    }
  }
]
4

3 回答 3

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
        }
}
于 2018-05-28T20:25:37.550 回答
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
    } 
})
于 2018-03-23T03:00:47.070 回答
0

您有 GroupBy 运算符,可以轻松完成此操作。这里需要按 companyName 和 companyId 有效载荷 groupBy $.companyName,companyId 分组

有关 groupby 运算符的详细信息,请阅读下面的链接。 https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators#group-by

于 2018-03-05T07:15:28.773 回答