JSON Payload ingested into MySQL database using Mule ESB
I tried with <foreach collection="#[message.payload]" doc:name="For Each">
as following:-
<db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="demo" password="Welcome1" database="demo" doc:name="MySQL Configuration"/> <flow name="seattleemergencyFlow1" doc:name="seattleemergencyFlow1"> <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8900" path="get-emergency" doc:name="HTTP"></http:inbound-endpoint> <http:outbound-endpoint exchange-pattern="request-response" host="data.seattle.gov" port="80" path="resource/kzjm-xkqj.json?" method="GET" contentType="application/json" doc:name="HTTP"></http:outbound-endpoint> <json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object"> </json:json-to-object-transformer> <foreach collection="#[message.payload]" doc:name="For Each"> <db:insert config-ref="MySQL_Configuration" doc:name="Database"> <db:parameterized-query><![CDATA[INSERT INTO emergencyrecords (incident_number, type, longitude) VALUES ( #[payload.incident_number], #[payload.type], #[payload.longitude])]]></db:parameterized-query> </db:insert> </foreach> </flow> </mule>
It was strange as only type value was returned and remaining value was null ..but when I removed the type from json like the following :-
[ { "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" } } ]
I got all the values of payload.incident_number
and payload.longitude
etc ..
It may be the reason that since
{ "type": " --T::00" }
is present at the beginning in the JSON it's only taking the value of "type":
as a list from all the values
So... The suggested solution is use the following format :-
[ { "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" } ]
by keeping "type" at the end .. you will get all the values as expected ..