Mule collection splitter with JSON

james picture james · Oct 1, 2012 · Viewed 11.8k times · Source

I have a JSON that looks something like the one pasted below. I am trying to extract each individual record and push it onto a queue. How would I extract each record in Mule? I've been trying to use the collection splitter and foreach loop, but I can't figure out how to get this to work.

{
  "locations": {
    "record": [
      {
        "id": 8817,
        "loc": "NEW YORK CITY"
      },
      {
        "id": 2873,
        "loc": "UNITED STATES"
      },
      {
        "id": 1501,
        "loc": "NEW YORK STATE"
      }
    ]
  }
}

Answer

David Dossot picture David Dossot · Oct 1, 2012

To do this:

  1. Transform the JSON entity to a hierarchy of Java structures
  2. Extract the record list
  3. Split the list

Now in Mule XML config:

<json:json-to-object-transformer returnClass="java.util.Map" />
<expression-transformer expression="#[payload.locations.record]" />
<collection-splitter />
<!-- TODO: dispatch to queue -->