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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Connect to Splunk Through Anypoint Studio in 10 Steps
  • Loading XML into MongoDB
  • Validate XML Request Against XML Schema in Mule 4
  • Transformations of Varying JSON Payloads Using Spark-Streaming

Trending

  • AI's Dilemma: When to Retrain and When to Unlearn?
  • How To Replicate Oracle Data to BigQuery With Google Cloud Datastream
  • Why We Still Struggle With Manual Test Execution in 2025
  • Scalable System Design: Core Concepts for Building Reliable Software
  1. DZone
  2. Coding
  3. Languages
  4. Payload Transformation: JSON to XML

Payload Transformation: JSON to XML

This tutorial tackles two different ways of transforming a JSON input payload into XML, from Dataweave to the JSON/XML Transformer Component.

By 
Christopher Co user avatar
Christopher Co
·
Apr. 18, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
30.3K Views

Join the DZone community and get the full member experience.

Join For Free

This is Part 2 of our Payload Transformation series; if you would like to check out the previous article about payload transformation, you can find it here. 

In this article, we are going to tackle the different ways of transforming a JSON input payload into XML. Let us start with the most basic.

Using Dataweave Component

Create The Flow

First, we need to define what our payload should look like.

To make this as simple as possible, we will define a basic JSON input.

Image title

Next, we will be creating a flow that looks like this:

Image title

In the flow we just created, we have defined

1. an HTTP inbound endpoint that accepts a JSON input

2. Transform Message Component (or Dataweave component) to transform our JSON input into XML.

In order to transform our JSON input to XML and make use of our Dataweave component, we can simply define our output in our Dataweave component as

Image title

Note:

1. We have defined our output as %output application/XML.

2. A root element student is defined.

 {    student:payload} 

Run The Program

The output should look like this:

Image title

Using JSON to XML Transformer Component

Create the Flow

Instead of using a Dataweave transformer component like we used on our first example, we are going to use JSON to XML transformer component.

Our new flow should look like this:

Image title

Let's try running our program using our first payload.

Image title

The generated output of our flow would be:

Image title

Only the first element is shown as output.

It is important to keep in mind that JSON and XML is not 1 is to 1 compatible, and also, JSON to XML transformer component relies on Staxon's JSON to XML conversion.

To workaround this issue, we must define a root element on our JSON input payload.

Our new payload should look like:

Image title

Let's try running our new program.

We finally have the following output:

Image title

Let's Try a More Complex Example

Transforming JSON Array into XML

What if we have the following JSON example as our input.

Image title

Instead of having a simple JSON, our input now has an array of subjects.

And, we would like to have the following as our XML output.

Image title

Create the Flow

We first must create a flow similar to our first example.

Image title

Again, we defined a Dataweave component to transform our JSON input.

Unlike our first example, we will make some changes on our Dataweave component.

Define Input JSON

Let's create a JSON file under src/main/resources named student.json

The file content for student.json should look like this:

Image title

Define Output XML

Let's create an XML file under src/main/resources named student.xml

The file content for student.xml should look like this:

Image title

Configure Dataweave Component

We can now configure our Dataweave component.

Let's start by setting our Dataweave component's input metadata - student.json

Image title

Then we will set the output metadata - student.xml

Image title

Our dataweave component's configuration should look like this:

Image title

Note: The map operator is used to apply transformation to each element in the collection. In this case, our collection is payload.subjects.

Another variation of the this solution will look like this:

Image title

Run the Program

Let's try running our new program.

We finally have the following output:

Image title

If you have any comment or suggestions that you would like to get tackled that were not mentioned in this article, please comment down below.

JSON XML Payload (computing)

Opinions expressed by DZone contributors are their own.

Related

  • How to Connect to Splunk Through Anypoint Studio in 10 Steps
  • Loading XML into MongoDB
  • Validate XML Request Against XML Schema in Mule 4
  • Transformations of Varying JSON Payloads Using Spark-Streaming

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!