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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  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.

Christopher Co user avatar by
Christopher Co
·
Apr. 18, 17 · Tutorial
Like (3)
Save
Tweet
Share
29.22K 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.

Popular on DZone

  • Tracking Software Architecture Decisions
  • Building Microservice in Golang
  • 11 Observability Tools You Should Know
  • Best CI/CD Tools for DevOps: A Review of the Top 10

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: