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