4

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"
      }
    ]
  }
}
David Dossot
  • 33,403
  • 4
  • 38
  • 72
james
  • 451
  • 2
  • 7
  • 12

3 Answers3

5

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 -->
Gibolt
  • 42,564
  • 15
  • 187
  • 127
David Dossot
  • 33,403
  • 4
  • 38
  • 72
  • 1
    When I do that I get the following error:Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token – james Oct 01 '12 at 22:00
  • I've run this config with the JSON payload you provided and ensured it works fine, so there must be something I'm unaware of in your environment. How did you pass the JSON payload to `vm://json.in`? – David Dossot Oct 01 '12 at 22:03
  • After the REST call I do the following and then your flow picks up: – james Oct 01 '12 at 22:34
  • Are you *sure* the payload that is sent to `vm://json.in` is *exactly* as the one you shown above? From the exception, it seems it starts with a JSON array not a JSON object as shown above. Also, I don't get the need for the `object-to-json-transformer`. Finally, you do not need to use a second flow with a VM endpoint, this was just an example I gave, you can have all in the same flow. Maybe it would be nice you actually also show your flow in your original question. – David Dossot Oct 01 '12 at 22:41
1

try this, instead of Map put List. That is working fine for me.

<json:json-to-object-transformer returnClass="java.util.List" />
<expression-transformer expression="#[message.payload.locations.record]" />
<collection-splitter />
Rajesh Narravula
  • 1,433
  • 3
  • 26
  • 54
1

I am adding one more solution in which returnClass="java.util.Map" works please have a look at code in which you can put the same JSON in the body using http method as POST while sending data from Fiddler or POST man client.

Here in this flow i am directly assigning expression in the Splitter instead of using Expression Transformer. I am using Any Point Studio to make it work.

    <flow name="mule-splitterFlow2" doc:name="mule-splitterFlow2">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP" path="splitterjson"/>
        <json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
        <splitter expression="#[message.payload.locations.record]" doc:name="Splitter">
        </splitter>
        <logger level="INFO" doc:name="Logger" message="#[message.payload]"/>
    </flow>
Utsav
  • 1,593
  • 4
  • 22
  • 46