Dataweave empty array input add default key:value

旧城冷巷雨未停 提交于 2020-01-07 02:55:11

问题


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"
  }]
}

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/44430542/dataweave-empty-array-input-add-default-keyvalue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!