hialu
hialu

Reputation: 23

MuleSoft DataWeave check if XML fild exists or not

I have the following mule DataWeave transformation:

([]) when (payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments =="") otherwise 
{
Id: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.@id as :string,
Date: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Date,
Time: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Time,
Cancel: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Cancel as :string,
VisitType: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#VisitType,
VisitTypeID: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#VisitTypeID as :string,
Duration: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Duration as :string,
Confirm: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Confirm as :string,
Providers: {
    Provider: {
        Id: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Providers.ns0#Provider.@id as :string,
        Name: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Providers.ns0#Provider.@name,
        Department: {
            Id: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Providers.ns0#Provider.ns0#Department.@id as :string,
            Name: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Providers.ns0#Provider.ns0#Department.@name,
            Center: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Providers.ns0#Provider.ns0#Department.@center,
            DepartmentDirections: "" when payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Providers.ns0#Provider.ns0#Department.ns0#DepartmentDirections == null otherwise payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Providers.ns0#Provider.ns0#Department.ns0#DepartmentDirections
        }
    }
},
PatientInstructions: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#PatientInstructions,
Copay: payload.ns0#GetFutureAppointmentsResponse.ns0#Appointments.ns0#Appointment.ns0#Copay as :string
}

Everything works except that the field "DepartmentDirections" doesn't always get passed in the XML. So now when i add this field my webservice doesn't return anything but when i remove it works. Since the field may or may not be there i don't think checking for null is going to help (in the scenario that it is not working the field is not there). Is there a way to easily check if the actual field exists (not the value) before assigning it?

Upvotes: 0

Views: 1341

Answers (1)

Abhay
Abhay

Reputation: 324

Add header skipNullOn="everywhere", it will skip the fields when transforming if not present in input. %output application/json skipNullOn="everywhere" Works only for XML and JSON

For reference: https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation

Upvotes: 1

Related Questions