Mule parsing with json part2

14
By Anirban Sen Chowdhary

Transcript of Mule parsing with json part2

Page 1: Mule parsing with json part2

By Anirban Sen Chowdhary

Page 2: Mule parsing with json part2
Page 3: Mule parsing with json part2

.

Page 4: Mule parsing with json part2

Before we start let’ see what JSON actually is …The formal JSON defination is :-JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999.

For more info visit :- http://json.org/

Page 5: Mule parsing with json part2

So, there can be different JSON payload ..One of the simple JSON payload is as follows :-[{"address":"10049 College Way N","longitude": "-122.335022","latitude": "47.701756","incident_number": "F110104009","type": "Aid Response","report_location": {"needs_recoding": false,"longitude": "-122.335022","latitude": "47.701756"}},{"address": "5929 Beach Dr Sw","longitude": "-122.397816","latitude": "47.550431","incident_number": "F110104008","type": "Aid Response","report_location":{"needs_recoding": false,"longitude": "-122.397816","latitude": "47.550431"}},{"type": " --T::00"}]

Note : JSON payloads are mainly used in REST web services

Page 6: Mule parsing with json part2

You can see this is a complex JSON request is formed as a list to parse….

So how can we parse it and extract the value of each components ?????

Page 7: Mule parsing with json part2

Now there are various ways to use this new component….I will show you small examples here

Page 8: Mule parsing with json part2

Let us consider following small example:-

Here in this flow we are setting the JSON in set payload component and extracting the values of each JSON attributes in logger

Page 9: Mule parsing with json part2

Corresponding Mule flow will be :-

<flow name="testFlow1" doc:name="testFlow1">

<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path=“parse” doc:name="HTTP"/>

<set-payload value="[{&quot;address&quot;:&quot;10049 College Way N&quot;,&quot;longitude&quot;: &quot;-122.335022&quot;,&quot;latitude&quot;: &quot;47.701756&quot;,&quot;incident_number&quot;: &quot;F110104009&quot;,&quot;type&quot;: &quot;Aid Response&quot;,&quot;report_location&quot;: {&quot;needs_recoding&quot;: false,&quot;longitude&quot;: &quot;-122.335022&quot;,&quot;latitude&quot;: &quot;47.701756&quot;}},{&quot;address&quot;: &quot;5929 Beach Dr Sw&quot;,&quot;longitude&quot;: &quot;-122.397816&quot;,&quot;latitude&quot;: &quot;47.550431&quot;,&quot;incident_number&quot;: &quot;F110104008&quot;,&quot;type&quot;: &quot;AidResponse&quot;,&quot;report_location&quot;:{&quot;needs_recoding&quot;: false,&quot;longitude&quot;: &quot;-122.397816&quot;,&quot;latitude&quot;: &quot;47.550431&quot;}},{&quot;type&quot;: &quot; --T::00&quot;}]" doc:name="Set Payload"/>

<json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object"/>

<foreach collection="#[message.payload]" doc:name="For Each"><logger message="#[payload.incident_number]" level="INFO" doc:name="Logger"/><logger message="#[payload.type]" level="INFO" doc:name="Logger"/><logger message="#[payload.longitude])]" level="INFO" doc:name="Logger"/>

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

</flow>

Page 10: Mule parsing with json part2

Here you can see I have used JSON to List transformer in the flow :- <json:json-to-object-transformerreturnClass="java.util.List" doc:name="JSON to List" />

And also instead of using a collection splitter to split the values of the list , this time we used a for loop

Page 11: Mule parsing with json part2

So, now let run the application and hit the url :-http://localhost:8081/parse to start the application

Page 12: Mule parsing with json part2

In the following you can see that the attributes value are extracted and displayed in logger in Mule console in following way :-

Page 13: Mule parsing with json part2

So, these are some more basic concepts of parsing a complex JSON payloads in Mule

Page 14: Mule parsing with json part2