Say I have a REST based service that returns a JSON object as under
{
"AlertDetails":
{
"start":0,
"left":0,
"_itemList":
[
{
"id":"badntp",
"text":"The time cannot be synchronized to the configured time server.",
"click":{
"text":"Click here to modify your time settings.",
"url":"datetime.html"
},
"severity":"warning",
"occurred":"20121031T10:18:54",
"args":null
},
{
"id":"updatesitefail",
"text":"The update site cannot be contacted to determine if software updates are available.",
"click":{
"text":"Click here to manually check for updates.",
"url":"http:\\\/\\\/xyz.com\\\/support\\\/xyz.html"
},
"severity":"info",
"occurred":"20121105T17:23:24",
"args":"[http:\\\/\\\/xyz.com\\\/support\\\/xyz.html]"
}
]
}
}
Now I have a table in PostgreSQL(say testTable) whose structure is as under
ID (string)
Severity (string)
Occurred (string)
How can I parse those values from the available JSON field to insert the same into the PostgreSQL Database from Mule ESB studio.
The final result in the testTable should be
ID Severity Occured
--------- --------- --------
"badntp" "warning" "20121031T10:18:54"
"updatesitefail" "info" "20121105T17:23:24"
My configuration file is as under
<jdbc:postgresql-data-source name="PostgreSQL_Data_Source"
user="username" password="pwd" url="jdbc:postgresql://localhost:5432/TestDB"
transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source" />
<jdbc:connector name="PostgreSQL_Connector" dataSource-ref="PostgreSQL_Data_Source"
validateConnections="true" queryTimeout="-1" pollingFrequency="0"
doc:name="Database">
<jdbc:query key="InsertRecord"
value="INSERT INTO "testTable"("ID","Severity","Occured") VALUES (?,?,?)" />
</jdbc:connector>
<flow name="testRestFlow1" doc:name="testRestFlow1">
<http:inbound-endpoint exchange-pattern="request-response"
address="http://localhost:8082/index.html" doc:name="HTTP" />
<http:rest-service-component httpMethod="GET"
serviceUrl="http://localhost:35798/RestServiceImpl.svc/json/567" />
<object-to-string-transformer />
<jdbc:outbound-endpoint exchange-pattern="one-way"
queryKey="InsertRecord" queryTimeout="-1" connector-ref="PostgreSQL_Connector"
doc:name="Database" />
</flow>
The #[message.payload] will contain the entire JSON string. What will come(regular expression or something else) in VALUES (?,?,?) to parse #[message.payload]?
Thanks in advance
From the MEL cheatsheet:
So use first to transform the JSON into datastructures (maps, lists):
Then we extract the
_itemListarray:Then we split this array in individual messages:
From there the following expressions will evaluate to the desired values so they can be used in the insert query: