sam
sam

Reputation: 97

Iterating through JSON list in Mulesoft

I've a JSON response that I'd like to iterate through and capture the values of incident
number, the longtitude, latitude as a group and send it to an SMS messaging service. I've tried different approaches but I'm not able to get to these elements. I've tried to debug and use
different expressions without any luck. Suggestions are appreciated.

[
  {
    "type": " --T::00"
  },
  {
    "address": "2720 E Madison St",
    "longitude": "-122.296667",
    "latitude": "47.623153",
   "incident_number": "F110104004",
    "type": "Medic Response",
    "report_location": {
    "needs_recoding": false,
    "longitude": "-122.296667",
    "latitude": "47.623153"
   }
  },
 {
    "address": "2260 1st Av S",
    "longitude": "-122.334199",
    "latitude": "47.583347",
    "incident_number": "F110103709",
    "type": "Aid Response",
    "report_location": {
    "needs_recoding": false,
    "longitude": "-122.334199",
    "latitude": "47.583347"
  }
},
{
   "address": "1930 Boren Av",
   "longitude": "-122.333103",
   "latitude": "47.617173",
   "incident_number": "F110103707",
   "type": "Aid Response",
   "report_location": {
    "needs_recoding": false,
    "longitude": "-122.333103",
    "latitude": "47.617173"
 }
]

Here's the configuration file.

<flow name="seattleemergencyFlow1" doc:name="seattleemergencyFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8900" path="get-emergency" doc:name="HTTP"/>
    <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"/>
    <json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.List"/>
</flow>

Upvotes: 0

Views: 4541

Answers (2)

Mohan
Mohan

Reputation: 520

you also convert json to xml data using

<json:json-to-xml-transformer doc:name="json-to-xml"/>

transformer and provide the xpath expression on for-each scope as below

<foreach collection="#[xpath('---')]" batchSize="--" doc:name="for-each-account-batch">

Upvotes: 0

Ryan Carter
Ryan Carter

Reputation: 11606

After converting the json to a List<Object> using the json:json-to-object-transformer as you have. You should be able to just use any of Mule's collection features such as the foreach scope/router and then MEL expressions to access specific fields:

<foreach>
        <logger level="ERROR" message="#[payload.address]" />
</foreach>

Upvotes: 1

Related Questions