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

Mule 使用Dataweave将多维数组转换为一维数组,mule,transformation,dataweave,mulesoft,Mule,Transformation,Dataweave,Mulesoft,需要通过重复父属性和子属性将多维数组(json)转换为一维数组。 条件是父母可能有孩子,也可能没有孩子。 有100个属性需要映射,如果我可以映射每个属性而不定义单个属性名(如果可行),那就太好了 如果只使用.dwl就可以解决这个问题,那就太好了 原始有效载荷: [ { "id": "1", "parentAttribute1": "parent1-1", "parentAttribute2": "parent1-2", "parentAttribute3":

需要通过重复父属性和子属性将多维数组(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函数,但无法合并它们

不适用


不适用

关键是使用展平加嵌套贴图。通过这种方式,您可以访问这两个级别,以便使用它们进行操作

%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]
        )
     )

Hi@kimmy看起来像是在输入和输出上粘贴了相同的json。请粘贴预期输出。谢谢@machaval。我已经更正了输出。好的,我会翻译的谢谢你,我试着在最后复制并粘贴你的代码(包括“)”,但无法让代码正常工作。获取多个错误。另外,我正在使用%dw 1.0进行转换。@kimmy我已经用dw 1的解决方案更新了解决方案谢谢您的帮助。一切都好。使用与dw完全相同的输入来满足这两个场景,但使用“parent.parentAttribute1”来获取特定属性,而不是“parent”。再次感谢。