conditional logic for dataweave code in mule 4

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật