Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Convert Payload From XML to Array of Objects by Comparing Mule 3 and Mule 4 Written Data Weave

DZone 's Guide to

Convert Payload From XML to Array of Objects by Comparing Mule 3 and Mule 4 Written Data Weave

See how to convert a payload from XML to an array of objects by comparing Mule 3 and 4.

· Integration Zone ·
Free Resource

Hello everyone, in this article, I'm going to share my experience of when I tried to do a conversion in Mule 4. I will briefly explain the purpose of this article while showing the code that I can implement in Mule 3 and how I have to write it in Mule 4 in order to get a similar response.

As we all know, Mule 4 is trending in the market, which makes Mule 3 people migrate their code from Mule 3 to Mule 4. 

My input response is in the form of an XML format.

Requirements

In my XML response, I have "line-item" as one field, which is in array format i.e., more than one set of "line-numbers". And "serial-list", which has a set of "serial-numbers". When I have to convert it in json/array on the bases of each line number and serial number, I have to create an array of objects. For example:

Input

<line-item>
<line-no>1</line-no>
<item-code>VZW-PIXELXL-B32G</item-code>
<ship-quantity>3.0</ship-quantity>
<unit-of-measure>EA</unit-of-measure>
<serial-list>
<serial-numbers>
<esn>584545835422125</esn>
</serial-numbers>
<serial-numbers>
<esn>584545835422127</esn>
</serial-numbers>
<serial-numbers>
<esn>584545835422126</esn>
</serial-numbers>
</serial-list>
<line-status/>
<base-price>699.99</base-price>
<bill-of-lading>531016550548</bill-of-lading>
<scac>FX2D</scac>
<special-message>
<special-message3>G-2PW2100-021-A</special-message3>
<special-message4>737.00</special-message4>
</special-message>
</line-item>
<line-item>
<line-no>2</line-no>
<item-code>TNEWMOTOG5PLUS64BLK</item-code>
<ship-quantity>3.0</ship-quantity>
<unit-of-measure>EA</unit-of-measure>
<serial-list>
<serial-numbers>
<esn>353023050087241</esn>
</serial-numbers>
<serial-numbers>
<esn>353023050087243</esn>

 Expected output

[
{
"OrderType": "OS",
"CustomerOrderNumber": "12542",
"ItemNumber": "",
"LotSerialNumber": "584545835422125",
"QuantityShipped": 1,
"UnitOfMeasure": "EA",
"ShipMethod": "FX2D",
"SupplierReference": "103755139",
"SupplierReference2": "",
"TrackingNumber": "531016550548",
"ShipDate": "20171101",
"ReplacementESN": "",
"LineNo": "1",
"VendorSKU": "VZW-PIXELXL-B32G"
},
{
"OrderType": "OS",
"CustomerOrderNumber": "12542",
"ItemNumber": "",
"LotSerialNumber": "584545835422127",
"QuantityShipped": 1,
"UnitOfMeasure": "EA",
"ShipMethod": "FX2D",
"SupplierReference": "103755139",
"SupplierReference2": "",
"TrackingNumber": "531016550548",
"ShipDate": "20171101",
"ReplacementESN": "",
"LineNo": "1",
"VendorSKU": "VZW-PIXELXL-B32G"
},
{
"OrderType": "OS",
"CustomerOrderNumber": "12542",
"ItemNumber": "",
"LotSerialNumber": "584545835422126",
"QuantityShipped": 1,
"UnitOfMeasure": "EA",
"ShipMethod": "FX2D",
"SupplierReference": "103755139",
"SupplierReference2": "",
"TrackingNumber": "531016550548",
"ShipDate": "20171101",
"ReplacementESN": "",
"LineNo": "1",
"VendorSKU": "VZW-PIXELXL-B32G"
},
{
"OrderType": "OS",
"CustomerOrderNumber": "12542",
"ItemNumber": "",
"LotSerialNumber": "353023050087241",
"QuantityShipped": 1,
"UnitOfMeasure": "EA",
"ShipMethod": "FX2D",
"SupplierReference": "103755139",
"SupplierReference2": "",
"TrackingNumber": "531016546431",
"ShipDate": "20171101",
"ReplacementESN": "",
"LineNo": "2",
"VendorSKU": "TNEWMOTOG5PLUS64BLK"
},
.
.
.
.
.
]

Dataweave Logic

In Mule 3, we generally need to use the flatten operator to do this conversion, as shown here:

%dw 1.0
%output application/json
---

flatten (payload.message.ship-advice.detail.*line-item map ((lineItem, lineItemIndex) ->
{
'lineItemXX': lineItem."serial-list".*"serial-numbers" map ((serialNumber, serialNumberIndex) -> {
OrderType :orderHeader."order-type",
.
.
.
})

}))..lineItemXX

In Mule 4, instead of one flatten operator, we require 2 flatten operators, as shown below:

%dw 2.0
output application/java
var msg = payload."message"
var shipHearder = msg."ship-advice"."header"
var shipInfo = shipHearder."shipment-information"
var poInfo = shipHearder."purchase-order-information"
var orderHeader = shipHearder."order-header"
---
flatten(flatten(payload."message"."ship-advice"."detail".*"line-item" map (lineItem, lineItemIndex) ->
{
'lineItemXX': lineItem."serial-list".*"serial-numbers" map ((serialNumber, serialNumberIndex) -> {
OrderType :orderHeader."order-type",
.
.
.

})

}).lineItemXX)

Reason Behind 2 Flatten Operators

In Mule 3, while using one flatten operator, it converts the XML payload into an array of objects on the bases of line numbers and serial numbers. But in the case of Mule 4, if we go for the same dataweave, which we wrote for Mule 3, the response will come as an array of arrays. To further convert an array of arrays into an array of objects, we used the 2nd flatten operator in Mule 4.

Topics:
mule 3 ,mule 4 ,integration ,tutorial ,dataweave

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}