4. 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/
5. So, there can be different JSON payload ..
One of the simple JSON payload is as follows :-
[
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
]
Note : JSON payloads are mainly used in
REST web services
6. You can see the JSON request is formed as a list where the
attributes like "category,"author”,"title“ and "price": 8.95 is
repeating in a list….
So how can we parse it and
extract the value of each
components ?????
7. Now there are various ways to use this new component….
I will show you small examples here
8. 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
9. Corresponding Mule flow will be :-
<flow name="testxmlFlow1" doc:name="testxmlFlow1">
<http:inbound-endpoint address="http://localhost:1081/order" exchange-
pattern="request-response" doc:name="HTTP" doc:description="Process HTTP reqests or
responses."/>
<set-payload value="[{ "category":
"reference","author": "Nigel Rees","title":
"Sayings of the Century","price": 8.95},{ "category":
"fiction","author": "Evelyn Waugh","title":
"Sword of Honour","price": 12.99}]" doc:name="Set Payload"/>
<json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to List"
/>
<collection-splitter doc:name="Collection Splitter"/>
<logger message="#[message.payload.category] and #[message.payload.author] and
#[message.payload.title] and #[message.payload.price]" level="INFO" doc:name="Logger"/>
</flow>
10. Here you can see I have used JSON to List transformer in the
flow :- <json:json-to-object-transformer
returnClass="java.util.List" doc:name="JSON to List" />
We have also used collection splitter to split the values of the
list :-
11. So, now let run the application and hit the url :-
http://localhost:1081/order to start the application
12. In the following you can see that the attributes value are extracted and
displayed in logger in Mule console in following way :-
13. In next slides I will bring you some more basic concepts of parsing
some more complex JSON payloads