[英]add null check for an array in dataweave
I am converting json to xml and i am getting array with one more inside array for that array, I am not able to set null check for inner array , getting script error in dataweave , i have attached sample Json request and XML response. 我正在将json转换为xml,并且正在使用该数组的另一个内部数组获取数组,我无法为内部数组设置null检查,在dataweave中获取脚本错误,我附加了示例Json请求和XML响应。
{"test":[ {
"GroupId": "3",
"forms": [{
"formId": "2"
} ]
},
{ "GroupId": "3"
]
} ]}
and this sample xml I am generating 我正在生成此示例xml
<test>
<myforms>
<GroupId>3</GroupId>
<formId>2</formId>
</myforms>
<myforms>
<GroupId>7</GroupId>
<formId>8</formId>
</myforms>
</test>
and My DW script is below 我的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
}
Issue Is :- I am not able put null check for inner array ie when payload.test.forms !=null it is throwing an script error , below snapshot with error mark of dataweave , Please tell how set null check for inner array loop 问题是 :-我无法对内部数组进行空检查,即,当payload.test.forms!= null时,它会在脚本下方抛出带有错误标记为dataweave的脚本错误,请告诉我如何对内部数组循环设置空检查
That's because of your syntax you are getting such errors. 那是因为您的语法,您得到这样的错误。
Can you try default []
, something like this : 您可以尝试使用default []
,如下所示:
($.forms default [] map {
formId:$.formId
})
That will help to get your expected result 那将有助于获得您的预期结果
Try the below code:- 试试下面的代码:
%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.