Search code examples
muleanypoint-studio

Mule - route depending on json payload


I'm running Mule CE 3.8 and need to route my messages depending on a value in the JSON payload. I get a list of JSON from a REST endpoint which runs through a JSON2ObjectList and a collection splitter.

    <http:request ... /> 
    <json:json-to-object-transformer returnClass="java.util.List" encoding="UTF-8" doc:name="JSON2ObjectList"/>
    <collection-splitter doc:name="Collection Splitter"/>
    <custom-transformer ../>

Now before the CustomTransformer and all the mapping I need to have a choice router that separates the messages depending on the payloads Product.Warehouse value. I was thinking of a Java component that could set a flow variable to the value of the Warehouse field but maybe there is an easier way to solve this? I looked into MEL and expression transformers but couldn't figure that one out.

Regards


Solution

  • You can put choice router before CustomTransformer and put check on payload.warehouse. As collection splitter will produce your Product Object. You can directly access its properties.

    Something like

        <json:json-to-object-transformer returnClass="java.util.List" encoding="UTF-8" doc:name="JSON2ObjectList"/>
        <collection-splitter doc:name="Collection Splitter"/>
        <choice doc:name="Choice">
            <when expression="#[payload.Warehouse == &quot;a&quot;]">
                <custom-transformer ../>
            </when>
            <when expression="#[payload.Warehouse == &quot;b&quot;]">
                <custom-transformer ../>
            </when>
        </choice>
    

    Hope this helps.