【问题标题】:Dataweave - iterating / transforming over flat structures with repeating key'sDataweave - 使用重复键迭代/转换平面结构
【发布时间】:2021-09-28 22:08:46
【问题描述】:

我在将 JSON 数组转换为新结构时遇到了困难,在这种结构中存在平面重复键,在这种情况下,即 Part 和 Labor。

[
  {
    "DisplayLineNumber": "1",
    "LineDescription": "Bumper cover",
    "Part": {
      "PartTypeCode": "PAN",
      "PriceColumn": "Standard",
      "PartCategory": "PARTSTD"
    },
    "Labor": {
      "LaborType": "LAB",
      "DBLaborHours": "1.9",
      "ActualLaborHours": "1.9",
      "LaborAmt": "0.00"
    },
    "Labor": {
      "LaborType": "LAR",
      "DBLaborHours": "2.6",
      "ActualLaborHours": "2.6",
      "LaborAmt": "0.00"
    }
  },
  {
    "DisplayLineNumber": "2",
    "LineAddedIndicator": "E01",
    "LineReference": "1",
    "SequenceNmbr": "3",
    "Transaction": "1",
    "LineDescription": "Add for Clear Coat",
    "LineSource": "DATABASE",
    "BettermentTaxedIndicator": "N",
    "Labor": {
      "LaborType": "LAR",
      "DBLaborHours": "0.0",
      "ActualLaborHours": "1.0",
      "LaborAmt": "0.00"
    },
    "Part": {
      "PartTypeCode": "PAN",
      "PriceColumn": "Standard",
      "PartCategory": "PARTSTD"
    }
  }
]

我正在尝试实现如下所示的结构。每个 Labor 或 Part 的对象数组,这些对象与 LineDescription 常见的更高级别对象相关。

[
  {
    "LineDescription": "Bumper cover",
    "LaborOrPart" : "Part",
    "PartTypeCode": "PAN",
    "PriceColumn": "Standard",
    "PartCategory": "PARTSTD"  
  },
  {
    "LineDescription": "Bumper cover",
    "LaborOrPart" : "Labor",
    "LaborType": "LAB",
    "DBLaborHours": "1.9",
    "ActualLaborHours": "1.9",
    "LaborAmt": "0.00"  
  },
  {
    "LineDescription": "Bumper cover",
    "LaborOrPart" : "Labor",
    "LaborType": "LAR",
    "DBLaborHours": "2.6",
    "ActualLaborHours": "2.6",
    "LaborAmt": "0.00" 
  },
  {
    "LineDescription": "Add for Clear Coat",
    "LaborOrPart" : "Labor",
    "LaborType": "LAR",
    "DBLaborHours": "0.0",
    "ActualLaborHours": "1.0",
    "LaborAmt": "0.00" 
  },
  {
    "LineDescription": "Add for Clear Coat",
    "LaborOrPart" : "Part",
    "PartTypeCode": "PAN",
    "PriceColumn": "Standard",
    "PartCategory": "PARTSTD" 
  }

]


任何关于如何使用 Dataweave 2.0 实现此类转换的见解将不胜感激。

谢谢

【问题讨论】:

    标签: arrays json dataweave mulesoft


    【解决方案1】:

    您可以通过提取每个元素中的键并添加键以轻松过滤它们,从原始元素中获取行描述并将所有映射在一起来做到这一点。一个不错的技巧是直接添加来自 Part 或 Labor 键的键值,因为如果它们不存在于元素中,它们将被忽略。

    %dw 2.0
    output application/json
    var validKeys=["Part", "Labor"]
    ---
    payload flatMap ((item, index) -> 
        (item pluck { key: $$, ($$):$ }) 
            filter  (validKeys contains $.key as String ) 
            map {
                LineDescription: item.LineDescription,
                LaborOrPart: $.key,
                ($.Part),
                ($.Labor) 
            }  
    ) 
    

    【讨论】:

      【解决方案2】:

      我使用了函数来重用和flatMap 进行映射。

      %dw 2.0
      output application/json
      fun mapLineItem (items = [], itemType, lineDescription) = (
          if (not isEmpty(items))
              (
                  items map ( {
                      LineDescription: lineDescription,
                      LaborOrPart: itemType
                  } ++ $)
              )
          else []
      )
      ---
      payload flatMap ((item) -> (
               mapLineItem(item.*Part,  "Part", item.LineDescription) ++ 
               mapLineItem(item.*Labor, "Labor", item.LineDescription)
      
      ))
      

      【讨论】:

        猜你喜欢
        • 2021-12-23
        • 1970-01-01
        • 2016-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-23
        • 1970-01-01
        相关资源
        最近更新 更多