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.
Join the DZone community and get the full member experience.
Join For FreeThis 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.
Next, we will be creating a flow that looks like this:
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
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:
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:
Let's try running our program using our first payload.
The generated output of our flow would be:
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:
Let's try running our new program.
We finally have the following output:
Let's Try a More Complex Example
Transforming JSON Array into XML
What if we have the following JSON example as our input.
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.
Create the Flow
We first must create a flow similar to our first example.
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:
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:
Configure Dataweave Component
We can now configure our Dataweave component.
Let's start by setting our Dataweave component's input metadata - student.json
Then we will set the output metadata - student.xml
Our dataweave component's configuration should look like this:
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:
Run the Program
Let's try running our new program.
We finally have the following output:
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.
Opinions expressed by DZone contributors are their own.
Comments