I need help on dataweave code. My requirement is to add the status field in the response based on the below criteria.
var manditory_field=”D6V_Cod,D6V,DYR_Cod,DYR”
If any of the manditory_field value is null in the request then status value will be “Certified-Retry”. If all manditory_field value is available then status value will be “Certified”.
note: manditory_field values is dynamic. later other field can be added.
input:
{
"vin": "1234",
"D6H": null,
"D6H_Cod": null,
"D6K": null,
"D6K_Cod": null,
"D6V": null,
"D6V_Cod": null,
"DCP": null,
"DCP_Cod": null,
"DI2": null,
"DI2_Cod": null,
"DJY": null,
"DJY_Cod": null,
"DXD": "04",
"DXD_Cod": "CD",
"DYD": null,
"DYD_Cod": null,
"DYR": null,
"DYR_Cod": null,
"DRC": null,
"DRC_Cod": null,
"DO9": null,
"DO9_Cod": null,
}
expected result:
{
"vin": "1234",
"D6H": null,
"D6H_Cod": null,
"D6K": null,
"D6K_Cod": null,
"D6V": null,
"D6V_Cod": null,
"DCP": null,
"DCP_Cod": null,
"DI2": null,
"DI2_Cod": null,
"DJY": null,
"DJY_Cod": null,
"DXD": "04",
"DXD_Cod": "CD",
"DYD": null,
"DYD_Cod": null,
"DYR": null,
"DYR_Cod": null,
"DRC": null,
"DRC_Cod": null,
"DO9": null,
"DO9_Cod": null,
"status": "Certified-Retry"
}
I modified @sudhish_s solution to check that every mandatory field is present in the payload (using every()
) and their values are not null.
%dw 2.0
import * from dw::core::Arrays
output application/json
var mandatory_fields = ["vin", "DXD"]
---
payload ++
if (mandatory_fields every ((namesOf(payload) contains ($)) and !(payload[$] is Null) ))
{
status: "Certified"
} else {
status: "Certified-Retry"
}
Output:
{
"vin": "1234",
"D6H": null,
"D6H_Cod": null,
"D6K": null,
"D6K_Cod": null,
"D6V": null,
"D6V_Cod1": null,
"DCP": null,
"DCP_Cod": null,
"DI2": null,
"DI2_Cod": null,
"DJY": null,
"DJY_Cod": null,
"DXD": "04",
"DXD_Cod": "CD",
"DYD": null,
"DYD_Cod": null,
"DYR": null,
"DYR_Cod": null,
"DRC": null,
"DRC_Cod": null,
"DO9": null,
"DO9_Cod": null,
"status": "Certified"
}
Here first I have tried to check if key in input payload matches the mandatory field along with null check for the matched key
Then further I checked if total mandatory key should match the satisfied fields from the input i.e. if both the count matches exactly then its Certified and if count does not match then Certified-Retry
%dw 2.0
output application/json
var manditory_field="D6V_Cod,D6V,DYR_Cod,DYR" splitBy ","
var conditionmatch=payload mapObject ((value, key, index) ->
do{
var a=(manditory_field filter(($ ~= (key)) and !isEmpty(value)))[0]
---
"matched": a
}
)
---
payload ++ if(sizeOf(valuesOf(conditionmatch) filter(!isEmpty($))) == sizeOf(manditory_field))
{
status: "Certified"
} else {
status: "Certified-Retry"
}
You can try with someEntry from Objects module to check if any value of mandatory fields are Null or not and update the status in the payload accordingly.
%dw 2.0
import someEntry from dw::core::Objects
var manditory_field = "D6V_Cod,D6V,DYR_Cod,DYR"
output application/json
---
payload ++ (if (payload filterObject (manditory_field contains $$) someEntry ((value, key) -> value is Null))
{
status: 'Certified-Retry'
}
else
{
status: 'Certified-Retry'
})
some
can be used to determine if any of the keys belongs to the list of fields
%dw 2.0
import * from dw::core::Objects
output application/json
var mandatory_fields = ["D6V_Cod1","D6V","DYR_Cod","DYR"]
---
payload ++ (
if (payload someEntry ((mandatory_fields contains ($$ as String)) and isEmpty($)))
{
status: "Certified-Retry"
} else {
status: "Certified"
}
)
3