JSON to JSON transformation in Mule

Facebooktwittergoogle_plusredditpinterestlinkedinmail

We often deals with JSON payloads in our application on a large scale, specially whenever we are exposing or consuming a REST API. One of the common scenario we get is to modify or transform the input JSON payload to another JSON format and produce it as an output.

If we take an simple example, let us consider we have the following JSON payload as an input for our application :-

{
    "name": "Anirban Sen Chowdhary",
    "id": 37
}

As you can see here, this is a simple JSON payload taken as an example, which contains only 2 elements, that is name and id.

So, if this JSON payload is coming into our application as an input and we require to transform/modify the payload into another JSON format and produce it as an output, something like the following :-

{
    "name": "Anirban Sen Chowdhary",
    "id": 37,
    "designation": "Director"
}

Where you can see the modified JSON has a new element designation as a third element . The challenge here is to modify the existing input JSON payload and to design an output JSON payload from it, which will contain this additional element in it with the same format.

So, the question is, how can be the input JSON format can be modified or transformed in another JSON format that we require, in a Mule application ??

How to transform or modify a JSON payload in Mule ??

The answer is Mule has a rich set of transformers and offers different options to transform form one format of payload into another. Mule also offers a powerful component called Datamapper, which is limited to Mule enterprise edition, and that can perform all these requirement in easy way.

But here for Mule community edition, we will choose a simplest  way of doing it. We will be using Expression Transformer for doing this.

Here is the following Mule flow, where we can transform the JSON input into another JSON format using Expression transformer :

code1

You can see above, whenever our Mule flow is receiving the input JSON in the flow, it is extracting all the element ( name and id ) from the JSON and storing it in variable.

After that, the Expression transformer comes into the picture, which is responsible in designing our output JSON payload and adding the element designation in it.

The graphical representation of this flow will be as follows :-

flow

Simple isn’t it ???

Testing our application

Now, we are done with our coding and will deploy our application and test it.

Once deployed on Mule server, we can test our application using RestClient or any other client you wish.

restClient1

So, we can see we are ready to test our Mule flow and the input JSON is already in the body which is going to be posted to our application.

once we click the send button we will get the following response in our client from our Mule flow :-

restClient2

Here, you can see here the input JSON has been modified and transformed into another JSON format as output, where the new element designation has been added.

Conclusion

So, you can find transforming or modifying a JSON payload into another JSON format is extremely easy in Mule using the Expression Transformer.

You can design your output JSON payload in any format you require using this component easily in your Mule flow and can create your dynamic JSON payload easily !!!

That’s all !!! I’ve hopefully been clear enough in displaying the implementation of Expression Transformer in designing and modifying your JSON payload which you can produce as an output from your api or application.

I hope you liked this !!!

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Anirban Sen Chowdhary

Anirban Sen Chowdhary is an information technology professional currently working on Java/J2ee, Esb and Integration platform. For more information, please visit http://anirbansenchowdhary.com and you can also follow https://twitter.com/Anir37

You may also like...

1 Response

  1. July 24, 2015

    […] of XML to XML format of data. If you have gone through my previous on JSON to JSON transformation here , you will realize how easy it is to perform such type of transformation in Mule. Here we will […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>