YTread Logo
YTread Logo

DataWeave Transformation Language in Mule Part-I | Transform JSON data into JSON or XML format

Jun 11, 2021
Hi guys, I'm Vishal. First of all, I would like to thank all my viewers and subscribers for showing such great response in my tutorials and as you suggested this tutorial, we will talk about Data View Trans

format

ion Language in a new version. I said that I will create a simple post flow that accepts a JSON request and then we will use the

data

view

language

to

transform

Jason's requirements into JSON and XML

format

. Ok so I have already created a project called

data

view tutorial and also created a simple post flow with the two components one is HTTP connector and second is a logger so let's go to the

mule

palette and find the message of

transform

ation and let's drag it between these two components, now inside the properties, I make it a little big. is bigger, I just close this Package Explorer, change the shape of this property, okay, this looks good inside this section, you can write your data view expression to transform your JSON requests into JSON or XML format, like this that here you can have one.
dataweave transformation language in mule part i transform json data into json or xml format
Note that the output type is Java Application, so I changed it to JSON elite, these two curly parentheses and type hair, that means I'm passing the assets from the request to the output response, so let's go ahead and let's run this project, right click on the project. run as

mule

app and save, I want to make sure that whatever the request and approval is, it should appear in my response, all good, my app started, let's go to postman. I am passing this JSON request to my flow. I click the sign button. You should get the same response in return, so inside the body, as you can see my moonstream has returned the same response since we are not doing any

transformation

logic.
dataweave transformation language in mule part i transform json data into json or xml format

More Interesting Facts About,

dataweave transformation language in mule part i transform json data into json or xml format...

Now I want to write a data view expression to retrieve only specific fields from this JSON request, so let's go to the transform message properties here there is an option called preview, if you click on this it will ask you for the required sample data to run the preview. What it does is that whatever mule expression you have written in this section, it will automatically show you the preview so you can understand what the data will be transformed into, so let's click on this to create required sample data. I click this action in the example, so I name it as a JSON request, click react type inside type is a simple, so I'll select it in the internal JSON instead of the schema, it should be a sample and here it will ask you to select the example file, so I go and select the example file that I have stored, so this is my example file.
dataweave transformation language in mule part i transform json data into json or xml format
Okay, now as you can see, it's giving me a preview of the expression that I've written inside this section. Now I will use the data view expression to transform this message. The first thing is I'm going to precede this payload with a tag called flights actually: so inside you can see the JSON request, all the responses you can say are wrapped inside this flights stack. Now I just delete this payload and put in the braces. He said these keys. I will read only specific fields. It's as if you want to read the price, origin, destination and total. seats available now within the flights first write my expression.
dataweave transformation language in mule part i transform json data into json or xml format
I want to give a description of my details, so I will write on a strip the name of the blackhead tag. You can do anything, so within the description value I will give it as flight details. oma so I want to bring the price to use the payload or autofill here you can select the price come on I want to bring the origin but you can change the name to oil say the start: payload then select between this autofill or region and then destiny. I'll give the name as the end with a dot below. destination finally I want to bring the total available seats subtotal is the payload point of Olin now inside the payload the total seats are available on the plane label so it will be the planned payload point of the total six selected save it now if you go to preview you can see the answer have been the first Aggies flights and then inside there are five tags description price start and total seats with their values ‚Äč‚Äčthis is how you can transform adjacent requests into another Jason now what's up if I want to transform it to an XML format so I can see that there is a mention of the output type as a slash from Jason Application, what if I change it to XML?
I get the XML form so the review will be added to my review and it will automatically give me the XML details now if I want to add more details to this. you can go ahead and add it like you also want to give receipts: payload oh this is adding one more type of MD since you want to add anything inside that flight section you can create your own tags let's say I want to give something as core. Like oh, you have to give and I open the label inside. I'll give a first preview on something like this and I'll give the name as flight and then a full value again inside that I'll read it from the payload. book, so basically it will create one more tag inside them that will add name value.
This is how you can convert any Jason request into Jason or XML format using the Data View Expression Language. In the next tutorial we will continue with the same topic and try to transform some complex data structure like a Jason matrix.

If you have any copyright issue, please Contact