[英]Unflatten a JSON Object into nested JSON Object using Dataweave 2.0
[英]Unflatten an Array in Mulesoft Dataweave 2.0
我有一組扁平的客戶,但找不到合適的方法(方式)來展開它,以便每個客戶及其年齡都嵌套。
輸入
{
"1st Customer": "2216",
"Age": "90",
"2nd Customer": "2231",
"Age": "90",
"3rd Customer": "2249",
"Age": "120",
"4th Customer": "2302",
"Age": "150",
"5th Customer": "",
"Age": ""
}
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,
}
]
}
在 Mule 中有一個很好的 function : divideBy 。 它將從 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
[
{
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: ""
}
]
非常感謝 Alex The 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] })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.