wso2wso2-esbwso2-integration-studio

Conditional Routing based on JSON evaluation in WSO2


I have following JSON data:

{
   "CustomerNames":{
      "Update":[
         {
            "CustName":"Name1",
            "id":"3"
         },
         {
            "CustName":"Name3",
            "id":"32"
         }
      ],
      "Create":[
         {
            "Name":"Name2"
         }
      ]
   }
}

If the Update JSONARray exists I need to call UpdateCustomer Sequence. And if the Create JSONArray exists I need to call CreateCustomer Sequence. I am trying to achieve this using Conditional Router Mediator. I have tried the below code for conditional routing:

<property expression="json-eval($.CustomerNames.Update)" name="CREATE_PAYLOAD" scope="default" type="STRING"/>
<property expression="json-eval($.CustomerNames.Create)" name="UPDATE_PAYLOAD" scope="default" type="STRING"/>

<conditionalRouter continueAfter="true">
                <conditionalRoute asynchronous="true" breakRoute="false">
                    <condition>
                        <match regex="true" source="boolean(get-property('CREATE_PAYLOAD'))"/>
                    </condition>
                    <target sequence="CREATE_CUSTOMER"/>
                </conditionalRoute>
                <conditionalRoute asynchronous="true" breakRoute="false">
                    <condition>
                        <match regex="true" source="boolean(get-property('UPDATE_PAYLOAD'))"/>
                    </condition>
                    <target sequence="UPDATE_CUSTOMER"/>
                </conditionalRoute>
            </conditionalRouter>

But this is not giving desired output. Am I doing anything wrong here?


Solution

  • The Conditional Router Mediator was removed from EI 6.5.0 onwards and the latest version doesn't support it. Therefore you may need to use the Switch Mediator to call the required sequences. In the Switch Mediator, if a matching case is found, it will be executed, and the remaining switch cases are not processed. Since you need to call both sequences, you can use two switch mediators as follows,

    <property expression="json-eval($.CustomerNames.Update)" name="CREATE_PAYLOAD" scope="default" type="STRING"/>
    <property expression="json-eval($.CustomerNames.Create)" name="UPDATE_PAYLOAD" scope="default" type="STRING"/>
    <switch source="boolean(get-property('CREATE_PAYLOAD'))">
        <case regex="true">
            <sequence key="CREATE_CUSTOMER"/>
        </case>
        <default/>
    </switch>
    <switch source="boolean(get-property('UPDATE_PAYLOAD'))">
        <case regex="true">
            <sequence key="UPDATE_CUSTOMER"/>
        </case>
        <default/>
    </switch>
    

    For more information check https://ei.docs.wso2.com/en/latest/micro-integrator/references/mediators/switch-Mediator/