dataweave 1.0 to 2.0 migration in mule

Viewed 70

Trying to convert the below dataweave from 1.0 to 2.0, but everything I've tried gives the following errors like

SkipNullon shows error

Usage of Namespacing is not accepted,

@PostalCode[0..4] is not valid and

can't access the value inside insuredInfo using insuredPrimaryAddr.

Dataweave 1.0:

%dw 1.0
%output application/xml skipNullOn="everywhere"
%var insuredInfo = payload.DTOApplication.DTOInsured
%var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.@PartyTypeCd == "InsuredParty")].*Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0]
%namespace ns0 http://decisionresearch.com/RateMaker
---
ns0#rate:{
    ns0#code:insuredPrimaryAddr,
    ns0#ZipCode: payload..Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0].@PostalCode[0..4] default ""
}

I tried Dataweave 2.0:

%dw 2.0
output application/xml skipNullOn="everywhere"
var insuredInfo = payload.DTOApplication.DTOInsured
var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.@PartyTypeCd == "InsuredParty")].*Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0]
namespace ns0 http://decisionresearch.com/RateMaker
---
ns0#rate:{
    ns0#code:insuredPrimaryAddr,
    ns0#ZipCode: payload..Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0].@PostalCode[0..4] default ""
}

payload : https://github.com/Manikandan99/rate-dtostep/blob/master/response.xml

Any ideas please on how to write the same in dataweave 2.0?

1 Answers

In dataweave 2.0 we use to operator for array indexing.

%dw 2.0
output application/xml skipNullOn="everywhere"
var insuredInfo = payload.DTOApplication.DTOInsured
var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.@PartyTypeCd == "InsuredParty")].*Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0]
ns ns0 http://decisionresearch.com/RateMaker
---
ns0#rate:{
    ns0#code:insuredPrimaryAddr,
    ns0#ZipCode: payload..Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0].@PostalCode[0 to 4] default ""
}

Output

<?xml version='1.0' encoding='windows-1252'?>
<ns0:rate xmlns:ns0="http://decisionresearch.com/RateMaker">
  <ns0:ZipCode>80003</ns0:ZipCode>
</ns0:rate>
Related