Mule parsing with json part2
-
Upload
anirban-sen-chowdhary -
Category
Technology
-
view
146 -
download
0
Transcript of Mule parsing with json part2
By Anirban Sen Chowdhary
.
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/
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
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 ?????
Now there are various ways to use this new component….I will show you small examples here
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
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="[{"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": "AidResponse","report_location":{"needs_recoding": false,"longitude": "-122.397816","latitude": "47.550431"}},{"type": " --T::00"}]" 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>
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
So, now let run the application and hit the url :-http://localhost:8081/parse to start the application
In the following you can see that the attributes value are extracted and displayed in logger in Mule console in following way :-
So, these are some more basic concepts of parsing a complex JSON payloads in Mule