在 DataWeave 中连接来自两个数组的值

0 json dataweave mulesoft

我刚刚开始使用 Dataweave 并试图弄清楚如何从这个特定的 JSON 响应转换数据。在相当详尽地阅读文档和搜索示例后,我很难过。找不到任何类似的东西。以下是我正在使用的有效载荷:

[
  {
    "columnMetadata": [
      {
        "name": "shape",
        "columnIndex": 0,
        "dataType": "string",
        "schemaType": "Static"
      },
      {
        "name": "color",
        "columnIndex": 1,
        "dataType": "string",
        "schemaType": "Static"
      }
    ],
    "rowData": [
      [
        "square",
        "yellow"
      ],
      [
        "circle",
        "green"
      ],
      [
        "star",
        "blue"
      ]
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

我试图实现的转变是这样的:

[
    {
        "shape": "square",
        "color": "yellow"
    },
    {
        "shape": "circle",
        "color": "green"
    },
    {
        "shape": "star",
        "color": "blue"
    }
]
Run Code Online (Sandbox Code Playgroud)

非常感谢任何帮助!

mac*_*val 5

解决这个问题的方法是使用动态对象。在这种情况下,此功能允许从其他对象动态组合一个对象或排列对象。它类似于 js 中的扩展运算符。

%dw 2.0
output application/json
---
payload flatMap ((item, index) -> do {
    var metadataNames =  item.columnMetadata map ((metadata, index) ->  metadata.name)
    ---
    item.rowData map ((datas, index) -> 
        {
            (
                datas map ((data, index) -> 
                    {
                        (metadataNames[index]):data
                    }
                )
            )
        }
    )
})
Run Code Online (Sandbox Code Playgroud)