【问题标题】:Unflatten an Array in Mulesoft Dataweave 2.0在 Mulesoft Dataweave 2.0 中展开数组
【发布时间】:2020-06-04 11:10:16
【问题描述】:

我有一组扁平的客户,但找不到合适的方法(方式)来取消扁平化,以便每个客户及其年龄都嵌套。

输入

{
 "1st Customer": "2216",
 "Age": "90",
 "2nd Customer": "2231",
 "Age": "90",
 "3rd Customer": "2249",
 "Age": "120",
 "4th Customer": "2302",
 "Age": "150",
 "5th Customer": "",
 "Age": ""
}

输出

{
 "customers": [
  {
   "CustomerSeq": "1",
   "CustomerID": "2216",
   "Age": 90,
  },
  {
   "CustomerSeq": "2",
   "CustomerID": "2231",
   "Age": 90,
  },
  {
   "CustomerSeq": "3",
   "CustomerID": "2249",
   "Age": 120,
  },
  {
   "CustomerSeq": "5",
   "CustomerID": "2302",
   "Age": 150,
  }
 ]
}

【问题讨论】:

  • 你输入的不是数组 Muhammad 而是一个对象,你的意思是要创建一个对象吗?

标签: mule dataweave mulesoft


【解决方案1】:

非常感谢,亚历克斯 divideBy 正是我想要的

解决方案[包括删除空值]:

%dw 2.0
output application/json

import * from dw::core::Objects
---
customers:
  ((payload filterObject ((value, key, index) -> value != "")) divideBy 2) map (
    (pCustomer, indexOfpCustomer) -> {
      CustomerSeq:indexOfpCustomer+1,
      CustomerID: pCustomer[0],
      Age: pCustomer[1]
    })

【讨论】:

    【解决方案2】:

    Mule 中有一个很好的功能:divideBy。 它将从对象中拆分出一组值,然后可以创建请求的对象:

    %dw 2.0
    var x={
     "1st Customer": "2216",
     "Age": "90",
     "2nd Customer": "2231",
     "Age": "90",
     "3rd Customer": "2249",
     "Age": "120",
     "4th Customer": "2302",
     "Age": "150",
     "5th Customer": "",
     "Age": ""
    }
    var keys=x pluck $$
    var values=x pluck $
    import * from dw::core::Arrays
    output application/dw
    ---
    values divideBy 2 map (item,index) -> 
     {
        CustomerSeq:index+1,
        CustomerID:item[0],
        Age:item[1]
     }
    

    输出

    [
      {
        CustomerSeq: 1,
        CustomerID: "2216",
        Age: "90"
      }, 
      {
        CustomerSeq: 2,
        CustomerID: "2231",
        Age: "90"
      }, 
      {
        CustomerSeq: 3,
        CustomerID: "2249",
        Age: "120"
      }, 
      {
        CustomerSeq: 4,
        CustomerID: "2302",
        Age: "150"
      }, 
      {
        CustomerSeq: 5,
        CustomerID: "",
        Age: ""
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-18
      • 2022-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多