json to json esb transformation

10
JSON to JSON transformation

Upload: domenico-schiavone

Post on 13-Apr-2017

68 views

Category:

Software


4 download

TRANSCRIPT

Page 1: Json to json esb transformation

JSON to JSON transformation

Page 2: Json to json esb transformation

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 transform the input JSON payload to another JSON format and produce it as an output.

Page 3: Json to json esb transformation

if we consider the following { "name": “John Black", "id": 37}This is a simple JSON payload taken as an

example, which contains only 2 elements, that

is name and id.

Page 4: Json to json esb transformation

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": “Rick Rosso", "id": 37, "designation": "Director"}

Page 5: Json to json esb transformation

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 ??

Page 6: Json to json esb transformation

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.

Page 7: Json to json esb transformation

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.

Page 8: Json to json esb transformation

<flow name="DynamicJSONFlow1" doc:name="DynamicJSONFlow1"> <http:inbound-endpoint exchange-pattern="request-response"

host="localhost" port="8085" path="test" doc:name="HTTP"/> <json:json-to-object-transformer returnClass="java.lang.Object"

doc:name="JSON to Object"/> <set-variable variableName="name"

value="#[message.payload.name]" doc:name="Variable"/> <set-variable variableName="id" value="#[message.payload.id]"

doc:name="Variable"/> <expression-transformer

Page 9: Json to json esb transformation

expression="#[[ 'name':flowVars.name, 'id':flowVars.id, 'designation':'Designation' ] ]" doc:name="Expression"/>

<json:object-to-json-transformer doc:name="Object to JSON"/> <logger level="INFO" message="#[message.payload]" doc:name="Logger"/> </flow>

Page 10: Json to json esb transformation