You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dataweave/Mule:CSV转JSON三级层级结构异常问题排查与修正

解决DataWeave重复键与层级结构错误问题

问题背景

将给定的4条CSV记录转换为三级层级结构JSON时,现有DataWeave代码生成的JSON出现「Duplicate key 'DEPT_ID'」错误,且jobData未按预期生成独立对象包裹的数组结构。

源CSV数据

EMP_ID,DEPT_ID,OPRT_UNIT_CODE,PROD_CODE,NODE_CODE,ALLOC_PCT
123456,00700,0101,000,00700-0101,5
123456,00700,0101,000,00700-0101-000,5
123456,02150,0101,000,2150-0101,95 
123456,02150,0101,000,02150-0101-000,95 

原始错误代码

%dw 2.0
output application/json
---
{
data:payload groupBy $.EMP_ID mapObject ((value, key) -> {
    empID: value[0].EMP_ID,
    jobData: [ value groupBy ($.EMP_ID ++ '|' ++ $.DEPT_ID ++ '|' ++ $.OPRT_UNIT_CODE ++ '|' ++ $.PROD_CODE) mapObject ((subValue, subKey) -> {
      // jobData: subKey,
      DEPT_ID: subValue[0].DEPT_ID, 
      OPRT_UNIT_CODE: subValue[0].OPRT_UNIT_CODE,
      PROD_CODE: subValue[0].PROD_CODE,
      orgData: subValue map ((item, index) -> {
      OrgCode: item.NODE_CODE,
        AllocPct: item.ALLOC_PCT
      })
    })
    ]
  })
} 

错误输出结果

{
  "data": {
    "empID": "123456",
    "jobData": [
      {
        "DEPT_ID": "00700",
        "OPRT_UNIT_CODE": "0101",
        "PROD_CODE": "000",
        "orgData": [
          {
            "OrgCode": "00700-0101",
            "AllocPct": "5"
          },
          {
            "OrgCode": "00700-0101-000",
            "AllocPct": "5"
          }
        ],
        "DEPT_ID": "02150",
        "OPRT_UNIT_CODE": "0101",
        "PROD_CODE": "000",
        "orgData": [
          {
            "OrgCode": "2150-0101",
            "AllocPct": "95"
          },
          {
            "OrgCode": "02150-0101-000",
            "AllocPct": "95"
          }
        ]
      }
    ]
  }
}

预期输出结果

{
  "data":{
    "empID": "123456",
    "jobData": [
      {
        "DEPT_ID": "00700",
        "OPRT_UNIT_CODE": "0101",
        "PROD_CODE": "000",
        "orgData": [
          {
            "OrgCode": "00700-0101",
            "AllocPct": "5"
          },
          {
            "OrgCode": "00700-0101-000",
            "AllocPct": "5"
          }
        ]
       },
       {
        "DEPT_ID": "02150",
        "OPRT_UNIT_CODE": "0101",
        "PROD_CODE": "000",
        "orgData": [
          {
            "OrgCode": "2150-0101",
            "AllocPct": "95"
          },
          {
            "OrgCode": "02150-0101-000",
            "AllocPct": "95"
          }
        ]
       }
    ]
  }
}

修正后的代码及解释

修正代码

%dw 2.0
output application/json
---
{
    data: payload groupBy $.EMP_ID mapObject ((value, key) -> {
        empID: value[0].EMP_ID,
        jobData: (value groupBy ($.EMP_ID ++ '|' ++ $.DEPT_ID ++ '|' ++ $.OPRT_UNIT_CODE ++ '|' ++ $.PROD_CODE) 
            mapObject ((subValue, subKey) -> {
                DEPT_ID: subValue[0].DEPT_ID, 
                OPRT_UNIT_CODE: subValue[0].OPRT_UNIT_CODE,
                PROD_CODE: subValue[0].PROD_CODE,
                orgData: subValue map ((item) -> {
                    OrgCode: item.NODE_CODE,
                    AllocPct: item.ALLOC_PCT
                })
            })) pluck $
    })
}

错误原因与修正点

  • 错误根源:原始代码中,内层groupBy后使用mapObject生成的是包含多个键值对的对象,直接将该对象放入数组[]中时,会把所有键值对合并到同一个对象里,导致出现重复的DEPT_ID、orgData等键。
  • 修正方式:使用pluck $将mapObject返回的对象转换为数组,提取对象中的每个值作为数组的独立元素,这样每个分组对应的结构就会成为jobData数组里的独立对象,既解决了重复键问题,也符合预期的层级结构。

内容的提问来源于stack exchange,提问作者Mahari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 00:24:52