[英]Unflatten an Array in Mulesoft Dataweave 2.0
I have a flat array of Customers, and could not find an appropriate Method (Way) to Unflatten it so that each Customer & his Age becomes Nested.我有一组扁平的客户,但找不到合适的方法(方式)来展开它,以便每个客户及其年龄都嵌套。
INPUT输入
{
"1st Customer": "2216",
"Age": "90",
"2nd Customer": "2231",
"Age": "90",
"3rd Customer": "2249",
"Age": "120",
"4th Customer": "2302",
"Age": "150",
"5th Customer": "",
"Age": ""
}
OUTPUT OUTPUT
{
"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,
}
]
}
There is a good function for it in Mule: divideBy .在 Mule 中有一个很好的 function : divideBy 。 It will split an array of your values from the object, and then the requested object could be created:它将从 object 中拆分出一组值,然后可以创建请求的 object:
%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]
}
output output
[
{
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: ""
}
]
Thanks a lot, Alex The divideBy is exactly what I was looking for非常感谢 Alex The divideBy正是我想要的
The Solution [including the removal of the empty values]:解决方案[包括删除空值]:
%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] })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.