简体   繁体   中英

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.

{"test":[ {
                   "GroupId": "3",
                  "forms": [{
                           "formId": "2"
                    } ]
    },
 {   "GroupId": "3"
           ]
    } ]}

and this sample xml I am generating

<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 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

在此处输入图片说明

That's because of your syntax you are getting such errors.
Can you try default [] , something like this :

($.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

                })
            }
        })
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM