I want to check whether all the required keys are present in the input JSON and filter out those that are not present. I tried the script but I am getting the below error. How to fix this?
Input:
{"req":{
"messageList": [
{
"res":1,
"id":"test"
}
]
}}
Required Keys:
var keys = ["res","req"]
DW Script:
%dw 2.0
output application/json
var keys = ["res"]
var p =payload.req.messageList[0]
fun getAllAbsentKeys(obj) = obj filterObject ((value, key, index) -> keys filter ((item, index) -> item == key) )
---
getAllAbsentKeys(p)
output:
["req"]
Cannot coerce Array ([]) to Boolean