【问题标题】:add null check for an array in dataweave在 dataweave 中为数组添加空检查
【发布时间】:2017-10-09 14:16:18
【问题描述】:

我正在将 json 转换为 xml,并且我正在为该数组获取一个包含另一个内部数组的数组,我无法为内部数组设置空检查,在 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 它抛出一个脚本错误,在带有数据编织错误标记的快照下方,请告诉如何为内部数组循环设置空检查

【问题讨论】:

    标签: arrays json xml mule dataweave


    【解决方案1】:

    这是因为您的语法导致您遇到此类错误。
    你可以试试default [],像这样:

    ($.forms default [] map {
    
                        formId:$.formId
    
                    })  
    

    这将有助于获得预期的结果

    【讨论】:

    【解决方案2】:

    试试下面的代码:-

    %dw 1.0
    %output application/xml
    ---
    {
        test : {
             (payload.test map {
                myforms : {
                    GroupId: $.GroupId as :number,
                     ($.forms default [] map {
    
                       formId : $.formId
    
                    })
                }
            })
        }
    }
    

    【讨论】:

    • 我已经实现了你的解决方案,但又犯了一个问题,你可以查看下面的网址了解更多详情forums.mulesoft.com/questions/78062/…Thnaks
    • 我不确定你是如何映射表单数组的,但是你可以在下面的 dw 中试一试.. 或者分享你正在使用的 dw 你的代码,这样更容易理解你是如何映射的它.. %dw 1.0 %output application/xml --- { test : { (payload.test map { myforms : { GroupId: $.GroupId as :number, ($.forms default [] map { formId : $.formId , (RoatationId : $.RoatationId) 当 $.RoatationId? 和 $.RoatationId !=null }) } }) } }
    • 感谢您的回复,让我检查一下
    猜你喜欢
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    相关资源
    最近更新 更多