11
Creating Dynamic JSON in Mule By Anirban Sen Chowdhary

Creating dynamic json

Embed Size (px)

Citation preview

Page 1: Creating dynamic json

Creating Dynamic JSON in Mule

By Anirban Sen Chowdhary

Page 2: Creating dynamic json

Mule ESB has multiple options to create dynamic payload. The payload used in Mule can be of multiple types like JSON, XML or simple Text payload.With Mule we can dynamically generate any type of payload at any point of time in the flow.We will see how to create a simple JSON payload dynamically within a Mule flow and post that JSON to an external service and get a response.It’s quite simple and we will see how we can generate a JSON in a flow.

Page 3: Creating dynamic json

How can we create JSON Dynamically? 

Page 4: Creating dynamic json

Let us consider we need to create a JSON file as following :-{ "myService": { "messageHeader": { "messageId": "ec75da88e64a739e12c126", "messageDateTime": "2014-09-18T14:07:46+0530" }, "mesageBody": { "myServiceRequest": { "Amount": 3520, "ConsumerId": "ABC-T14005-AFF", "Timestamp": "2014-10-27T18:36:18+0700" } } }}

Now, we need to create this flow in our Mule flow and send the JSON request created to the external service and will get a response. We will assign the JSON elements value from flow variables for now, but you can use message inbound properties or other ways to assign the flow variables dynamically within the flow.

Page 5: Creating dynamic json

We will use Mule Expression Transformer to create this JSON request. As per Mule documentation here https://developer.mulesoft.com/docs/display/current/Expression+Transformer+Reference :-The Expression Transformer executes one or more expressions on the current message. The results of these expressions becomes the payload of the current message.

Page 6: Creating dynamic json

Let’s consider our Mule flow as following :-

Here you can see, the expression transformer is creating the JSON request which is send to the external web service. The flow variables are used to set the value and you can assign the variables dynamically in your way.

Page 7: Creating dynamic json

Now let’s test our flow by placing by hitting the url http://localhost:8085/rc/test on browser as follows, :-

The JSON request will be generated in the flow and with that request it will be posted to the external web service which will return a response

Page 8: Creating dynamic json

Now, we can see in our logger the JSON request generated and the JSON response from the external service in return :-

Page 9: Creating dynamic json

So, here you can see how to set a Dynamic JSON and can be send to the external service to get a response…. you can try this with your requirement as well and set the value dynamically in the flow variables or can use other dynamic options like inbound properties etc !!!….

Page 10: Creating dynamic json

In my next slide I will bring some other techniques in Mule implementation .Hope you have enjoyed this simpler version. Keep sharing your knowledge and let our Mule community grow

Page 11: Creating dynamic json

Thank You