Search code examples
mulemule-studiomule-el

Mule - split a big JSON list into multiple smaller JSON lists


I have a list of json objects containing about 200 objects. I want to split that list into smaller lists where each list contains max 20 objects each. I would like to POST each sublist to HTTP based endpoint.

<flow name="send-to-next-step" doc:name="send-to-vm-flow">
    <vm:inbound-endpoint exchange-pattern="one-way"
        path="send-to-next-step-vm" doc:name="VM" />
    <!-- received the JSON List payload with 200 objects-->
    <!-- TODO do processing here to split the list into sub-lists and call sub-flow for each sub-list
    <flow-ref name="send-to-aggregator-sf" doc:name="Flow Reference" />
</flow>

One possible way is that I write a java component which iterates over the list and after iterating over each 20 objects, call sub-flow. Is there any better way of accomplishing this?


Solution

  • If your payload is a Java Collection, the Mule foreach scope has batching built in: http://www.mulesoft.org/documentation/display/current/Foreach

    Example:

    <foreach batchSize="20">
       <json:object-to-json-transformer/>
       <http:outbound-endpoint ... />
    </foreach>