1

需要通过重复父属性与子属性将多维数组(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",
  }
]

尝试使用 reduce、group 函数,但无法合并它们。

不适用

不适用

4

1 回答 1

2

关键是使用 flatten 加上一个嵌套的 map。通过这种方式,您可以访问这两个级别,以便您可以使用它们进行操作。

%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]
        )
     )
于 2019-06-30T15:32:27.250 回答