[英]Mule Dataweave null check and array empty check of JSON payloads
[英]add null check for an array in dataweave
我正在將json轉換為xml,並且正在使用該數組的另一個內部數組獲取數組,我無法為內部數組設置null檢查,在dataweave中獲取腳本錯誤,我附加了示例Json請求和XML響應。
{"test":[ {
"GroupId": "3",
"forms": [{
"formId": "2"
} ]
},
{ "GroupId": "3"
]
} ]}
我正在生成此示例xml
<test>
<myforms>
<GroupId>3</GroupId>
<formId>2</formId>
</myforms>
<myforms>
<GroupId>7</GroupId>
<formId>8</formId>
</myforms>
</test>
我的DW腳本在下面
%dw 1.0
%output application/xml
---
{
(test: {
(payload.test map {
myforms: {
GroupId: $.GroupId as :number,
(($.forms map {
formId:$.formId
})) when payload.test.forms !=null
}
})
}) when payload.test !=null
}
問題是 :-我無法對內部數組進行空檢查,即,當payload.test.forms!= null時,它會在腳本下方拋出帶有錯誤標記為dataweave的腳本錯誤,請告訴我如何對內部數組循環設置空檢查
那是因為您的語法,您得到這樣的錯誤。
您可以嘗試使用default []
,如下所示:
($.forms default [] map {
formId:$.formId
})
那將有助於獲得您的預期結果
試試下面的代碼:
%dw 1.0
%output application/xml
---
{
test : {
(payload.test map {
myforms : {
GroupId: $.GroupId as :number,
($.forms default [] map {
formId : $.formId
})
}
})
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.