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

Payload Transformation: JSON to XML

DZone's Guide to

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.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

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.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
integration ,xml ,json ,tutorial ,dataweave

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}