在 Dataweave 中需要帮助
Need Help in Dataweave
谁能帮我把这个 dataweave 转换成目标格式?基本上将项目转换为数组。
我什至开始了,但不确定如何继续。任何建议都会有所帮助
payload mapObject(order,key,index) -> {
((key):order) if ! (key contains "Item_"),
}
输入:
{
"id": "1",
"Item_1_ID": "43-583-0978",
"Item_1_Name": "Sodium Chloride",
"Item_1_Quantity": "26",
"Item_1_Price": "802.41",
"Item_2_ID": "71-788-5293",
"Item_2_Name": "Ciprofloxacin",
"Item_2_Quantity": "100",
"Item_2_Price": "608.64",
}
输出:
{
"id": "1",
Items:[{
"ID":"43-583-0978",
"Name": "Sodium Chloride",
"Price": "802.41",
"Quantity": "26",
},
{
"ID":"71-788-5293",
"Name": "Ciprofloxacin",
"Price": "100",
"Quantity": "608.64",
}]
}
这里有您所请求问题的解决方案。它使用一些正则表达式来提取键的部分,然后 groupBy 和 pluck 函数按 Id 编号进行分组,并将 groupBy 结果转换为数组并转换键。
%dw 2.0
output application/json
fun getItemNumber(keyName:String) =
(keyName scan /Item_(\d+)/)[0][1]
fun getKeyName(keyName:String) =
(keyName scan /Item_[\d+]_(.*)/)[0][1]
---
{
"Id" : payload.id,
"Item": (payload - "id")
groupBy ((value, key) -> //Group it by ID
getItemNumber(key as String)
)
pluck ((value, key, index) -> //Go through all the values of the groups and take the real keys
value mapObject ((value, key, index) -> {
(getKeyName(key)): value
})
)
}
谁能帮我把这个 dataweave 转换成目标格式?基本上将项目转换为数组。
我什至开始了,但不确定如何继续。任何建议都会有所帮助
payload mapObject(order,key,index) -> {
((key):order) if ! (key contains "Item_"),
}
输入:
{
"id": "1",
"Item_1_ID": "43-583-0978",
"Item_1_Name": "Sodium Chloride",
"Item_1_Quantity": "26",
"Item_1_Price": "802.41",
"Item_2_ID": "71-788-5293",
"Item_2_Name": "Ciprofloxacin",
"Item_2_Quantity": "100",
"Item_2_Price": "608.64",
}
输出:
{
"id": "1",
Items:[{
"ID":"43-583-0978",
"Name": "Sodium Chloride",
"Price": "802.41",
"Quantity": "26",
},
{
"ID":"71-788-5293",
"Name": "Ciprofloxacin",
"Price": "100",
"Quantity": "608.64",
}]
}
这里有您所请求问题的解决方案。它使用一些正则表达式来提取键的部分,然后 groupBy 和 pluck 函数按 Id 编号进行分组,并将 groupBy 结果转换为数组并转换键。
%dw 2.0
output application/json
fun getItemNumber(keyName:String) =
(keyName scan /Item_(\d+)/)[0][1]
fun getKeyName(keyName:String) =
(keyName scan /Item_[\d+]_(.*)/)[0][1]
---
{
"Id" : payload.id,
"Item": (payload - "id")
groupBy ((value, key) -> //Group it by ID
getItemNumber(key as String)
)
pluck ((value, key, index) -> //Go through all the values of the groups and take the real keys
value mapObject ((value, key, index) -> {
(getKeyName(key)): value
})
)
}