使用Dataweave将多维数组转换为一维数组

时间:2019-06-29 10:33:37

标签: mule transformation dataweave mulesoft

需要通过将父属性与子属性重复来将多维数组(json)转换为一维数组。 条件是父母可能有孩子或没有孩子。 有100多个属性需要映射,因此如果我可以映射每个属性而无需定义单个属性名称(如果可行),那将会很棒。

如果仅通过使用.dwl可以解决此问题,那就太好了 原始有效载荷:

[
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "parentAttribute2": "parent1-2",
    "parentAttribute3": "parent1-3",
    "child": [
      {
        "childAttribute1": "inner1-1-1",
        "childAttribute2": "inner1-1-2"
      },
      {
        "childAttribute1": "inner1-2-1",
        "childAttribute2": "inner1-2-2"
      },
      {
        "childAttribute1": "inner1-3-1",
        "childAttribute2": "inner1-3-2"
      }
    ]
  },
  {
    "id": "2",
    "parentAttribute1": "parent2-1",
    "parentAttribute2": "parent2-2",
    "parentAttribute3": "parent2-3",
    "child": [
      {
        "childAttribute1": "inner2-1-1",
        "childAttribute2": "inner2-1-2"
      }
    ]
  },
  {
    "id": "3",
    "parentAttribute1": "parent3-1",
    "parentAttribute2": "parent3-2",
    "parentAttribute3": "parent3-3"
  }
]

翻译后预期的有效载荷-方案1-所有属​​性

[
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "parentAttribute2": "parent1-2",
    "parentAttribute3": "parent1-3",
    "childAttribute1": "inner1-1-1",
    "childAttribute2": "inner1-1-2"
  },
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "parentAttribute2": "parent1-2",
    "parentAttribute3": "parent1-3",
    "childAttribute1": "inner1-2-1",
    "childAttribute2": "inner1-2-2"
  },
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "parentAttribute2": "parent1-2",
    "parentAttribute3": "parent1-3",
    "childAttribute1": "inner1-3-1",
    "childAttribute2": "inner1-3-2"
  },
  {
    "id": "2",
    "parentAttribute1": "parent2-1",
    "parentAttribute2": "parent2-2",
    "parentAttribute3": "parent2-3",
    "childAttribute1": "inner2-1-1",
    "childAttribute2": "inner2-1-2"
  },
  {
    "id": "3",
    "parentAttribute1": "parent3-1",
    "parentAttribute2": "parent3-2",
    "parentAttribute3": "parent3-3"
  }
]

翻译后预期的有效载荷-方案2-仅某些属性

[
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "childAttribute1": "inner1-1-1"
  },
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "childAttribute1": "inner1-2-1"
  },
  {
    "id": "1",
    "parentAttribute1": "parent1-1",
    "childAttribute1": "inner1-3-1"
  },
  {
    "id": "2",
    "parentAttribute1": "parent2-1",
    "childAttribute1": "inner2-1-1"
  },
  {
    "id": "3",
    "parentAttribute1": "parent3-1",
  }
]

试图使用归约,分组功能,但无法合并它们。

n / a

n / a

1 个答案:

答案 0 :(得分:2)

关键是使用展平图和嵌套地图。这样,您可以访问两个级别,因此可以对其进行操作。

%dw 2.0
output application/json  
---
flatten(payload map 
    ((parent, index) -> 
        if (parent.child?)
            parent.child map ((child, index) -> (parent - "child") ++ child)
        else
          [parent]
    )
 )

对于dw 1,这是解决方案

%dw 1.0

    %output application/json  
    ---
    flatten (payload map 
        ((parent, index) -> 

                parent.child map ((child, index) -> (parent - "child") ++ child) when  (parent.child?)
            otherwise
              [parent]
        )
     )