[英]Mule transform message move out list of objects from array to objects
ule子轉換消息:
%dw 1.0
%function removeEmptyInArray(arr) arr map (
(removeEmptyInArray($) when $ is :array
otherwise (removeEmptyInObject($) when $ is :object
otherwise $ when ($ != null and (sizeOf $) > 0) otherwise null))
) when arr != []
otherwise null
%function removeEmptyInObject(obj) obj mapObject (
'$$': (removeEmptyInArray($) when $ is :array
otherwise (removeEmptyInObject($) when $ is :object
otherwise $ when ($ != null and (sizeOf $) > 0) otherwise null))
)
%output application/json skipNullOn="everywhere" , encoding='UTF-8'
%namespace ns0 http://xxxxx
%namespace ns1 http://xxxxx
%function getKey(key) (capitalize key) replace /\s/ with '' replace /Tns:/ with '' replace /tns:/ with ''
%function convert(obj) obj mapObject {
(getKey($$)) : convert($) when ($ is :object) otherwise $
}
---
{
"Header": {
"H1": {
"ID": sessionVars.ID ,
"UID": sessionVars.UID
}
},
"Body": removeEmptyInObject(convert(payload))
}
對策:
第一回應:
"Details": [{
"A": {
"A1": {
"A11": ""
},
"A2": {
}
"A3": ""
}
},
{
"B": {
"B1": {
"B11": ""
},
"B2": ""
}
}]
第二回應:
"Details": {
"A": {
"A1": {
"A11": ""
},
"A2": ""
}
}
這兩個響應來自不同的人,因此響應可以非常基於ID。
但是我需要每次響應都應以不像Array []的對象的形式出現,並且我也不能對整個有效負載進行硬編碼,因為可以根據請求更改有效負載。
第一種請求的預期響應:
“ Details”:“ A”:{“ A1”:{“ A11”:“”},“ A2”:{
}
"A3": ""
}
},
{
"B": {
"B1": {
"B11": ""
},
"B2": ""
}
}
如果您只是想服用:
{
"Details": [
{
"A1": {}
},
{
"A2": {}
}
]
}
並將其轉換為:
{
"Details": {
"A1": {},
"A2": {}
}
}
換句話說,將數組“減少”到對象:您可以通過reduce
:
payload.Details reduce ((detail, obj={}) ->
obj ++ detail
)
或者您可以使用速記:
payload.Details reduce $$ ++ $
這將在此處詳細說明。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.