Dataweave - 使用重复键迭代/转换平面结构

Dataweave - iterating / transforming over flat structures with repeating key's

我在将 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 实现此类转换的见解都将不胜感激。

谢谢

您可以通过提取每个元素中的键并添加键来轻松过滤它们,从原始元素中获取行描述并将它们映射在一起来实现。一个不错的技巧是直接从 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) 
        }  
) 

我已经使用函数来重用,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)

))