2. 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.
3. if we consider the following
{
"name": “Mike White",
"id": 37
}
This is a simple JSON payload taken as an example,
which contains only 2 elements, that
is name and id.
4. 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": "John Rosso",
"id": 37,
"designation": "Director"
}
5. 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 ??
6. 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.
7. 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.
So, you can transform from one JSON to another
dynamically using Expression transformer in the
following flow.