首页 > 解决方案 > 如何使用 Dataweave 将两个数组拆分为对象

问题描述

我正在从数据库中获取数据并尝试使用 dataweave 进行分配。但是,我查看是否为查询获取多个值,然后它将列的所有值组合到单个数组中。

目前使用我的数据编织逻辑它返回我这个输出

电流输出:

"orders": [
        {
          "orderID": "[10355, 10383, 10453]",
          "employeeID": "[6, 8, 1]"
        }
      ]

预期输出:

"orders": [
            {
              "orderID": "103553",
              "employeeID": "6"
            },
            {
              "orderID": "10383",
              "employeeID": "8"
            },
            {
              "orderID": "10453",
              "employeeID": "1"
            }
          ]

提前致谢

标签: jsonmuledataweave

解决方案


您可以即时将 String 更改为 JSON,然后使用它 https://simpleflatservice.com/mule4/ChangeStringToJsonOnTheFly.html

%dw 2.0
var x={"orders": [
        {
          "orderID": "[10355, 10383, 10453]",
          "employeeID": "[6, 8, 1]"
        }
      ]    
}
output application/json
---
{
    orders: read(x.orders[0].orderID,'application/json') map (item,index) ->
    {
        orderID:item, 
        "employeeID": read(x.orders[0].employeeID,'application/json')[index]
    }
}

输出

{
  "orders": [
    {
      "orderID": 10355,
      "employeeID": 6
    },
    {
      "orderID": 10383,
      "employeeID": 8
    },
    {
      "orderID": 10453,
      "employeeID": 1
    }
  ]
}

推荐阅读