Dataweave empty array input add default key:value

2019-09-12 10:22发布

is there a way to map a http response which is in json array to another json map also considering default values i.e when it is empty map a default key pair?

input

{

"key1": []
"key2":[x,y]
}

req output

{"table":[{
  "a-key1": "deafault-value",
  "a-key2": "x",
  "b-key2": "y"
  }]
}

2条回答
祖国的老花朵
2楼-- · 2019-09-12 10:31

You can use default keyword only in case of null but for blank and empty array you can use something like

%dw 1.0
%output application/json
%function getDefault(inputdata, defaultvalue)  inputdata when inputdata != null and (sizeOf inputdata) > 0 otherwise defaultvalue
---
{"table": [{
  "a-key1": getDefault(payload.key1,"defaultKey1") ,
  "a-key2": getDefault(payload.key2,"defaultKey2"),
  "b-key2": getDefault(payload.key3,"defaultKey2")

  }]
}

assume payload is header or query params.

Hope this helps.

查看更多
走好不送
3楼-- · 2019-09-12 10:49
<dw:transform-message doc:name="Transform Message" metadata:id="....">
<dw:input-payload mimeType="application/xml"/>
<dw:set-payload>
<![CDATA[%dw 1.0
%output application/xml
---
{
    table: {
        (payload.*object map ( {
            key: $.value when $.value != empty
                 otherwise 'deafault-value'
        }))
    }
}
]]>
</dw:set-payload>
</dw:transform-message>
查看更多
登录 后发表回答