0

在 DWL 2.0 中执行 groupBy 操作后,我需要从实际数据中删除 groupBy 字段及其值。

例如

我有带有 json 数组的有效负载,例如:

[
  {
    "empId": 1,
    "empName": "James",
    "deptName": "Sales"
  },
  {
    "empId": 2,
    "empName": "ABC",
    "deptName": "Marketing"
  },
  {
    "empId": 3,
    "empName": "BCD",
    "deptName": "Account"
  },
  {
    "empId": 4,
    "empName": "EFG",
    "deptName": "Sales"
  },
  {
    "empId": 5,
    "empName": "HIJK",
    "deptName": "Account"
  },
  {
    "empId": 6,
    "empName": "LMN",
    "deptName": "Sales"
  },
  {
    "empId": 7,
    "empName": "OPQR",
    "deptName": "Marketing"
  }
]

在这个添加的转换上执行 groupBy -->

<ee:transform doc:name="Transform Message">
    <ee:message>
        <ee:set-payload>
        <![CDATA[%dw 2.0
                    output
                    application/json
                    ---                 
                    (payload groupBy (item) -> item.customerId) 
         ]]></ee:set-payload>
    </ee:message>
</ee:transform>

这种转换的结果是 -->

{
  "Sales": [
    {
      "empId": 1,
      "empName": "James",
      "deptName": "Sales"
    },
    {
      "empId": 4,
      "empName": "EFG",
      "deptName": "Sales"
    },
    {
      "empId": 6,
      "empName": "LMN",
      "deptName": "Sales"
    }
  ],
  "Marketing": [
    {
      "empId": 2,
      "empName": "ABC",
      "deptName": "Marketing"
    },
    {
      "empId": 7,
      "empName": "OPQR",
      "deptName": "Marketing"
    }
  ],
  "Account": [
    {
      "empId": 3,
      "empName": "BCD",
      "deptName": "Account"
    },
    {
      "empId": 5,
      "empName": "HIJK",
      "deptName": "Account"
    }
  ]
}

但是当您查看结果时,实际数据 json 中不再需要 depetName 标记。我想从数据 json 中删除这个元素,并希望得到最终结果,例如 -->

{
  "Sales": [
    {
      "empId": 1,
      "empName": "James"
    },
    {
      "empId": 4,
      "empName": "EFG"
    },
    {
      "empId": 6,
      "empName": "LMN"
    }
  ],
  "Marketing": [
    {
      "empId": 2,
      "empName": "ABC"
    },
    {
      "empId": 7,
      "empName": "OPQR"
    }
  ],
  "Account": [
    {
      "empId": 3,
      "empName": "BCD"
    },
    {
      "empId": 5,
      "empName": "HIJK"
    }
  ]
} 

任何人都可以帮我解决这个问题吗?

4

1 回答 1

1

您可以使用 mapObject 函数重新映射 groupBy 的结果,然后映射值组结果并删除每个对象中的字段

output application/json
---                 
payload groupBy ((item) -> item.deptName) 
        mapObject ((value, key, index) -> 
            {
                (key): value map ((item, index) -> item - "deptName")
            })
于 2019-11-19T20:33:48.670 回答