DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • SmartXML: An Alternative to XPath for Complex XML Files
  • How to Convert CSV to XML in Java
  • How to Connect to Splunk Through Anypoint Studio in 10 Steps
  • FHIR Data Model With Couchbase N1QL

Trending

  • The Future of Java and AI: Coding in 2025
  • How to Create a Successful API Ecosystem
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • Code Reviews: Building an AI-Powered GitHub Integration
  1. DZone
  2. Data Engineering
  3. Data
  4. Convert Payload From XML to Array of Objects by Comparing Mule 3 and Mule 4 Written Data Weave

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.

By 
Sipra Nayak user avatar
Sipra Nayak
·
Jun. 11, 19 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
17.9K Views

Join the DZone community and get the full member experience.

Join For Free

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.

XML Data structure Convert (command) Object (computer science) Payload (computing) Data (computing) Weave (protocol)

Opinions expressed by DZone contributors are their own.

Related

  • SmartXML: An Alternative to XPath for Complex XML Files
  • How to Convert CSV to XML in Java
  • How to Connect to Splunk Through Anypoint Studio in 10 Steps
  • FHIR Data Model With Couchbase N1QL

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!