bfury
bfury

Reputation: 99

Combine json responses from iterate mediator in WSO2

I have the following json which I iterate in order to get each token and id:{ "Response": { "Data": [{ "Token": "12345", "Code": "10148117" }, { "Token": "123465", "Code": "10148118" }] } }

<iterate expression="//Data" id="it1">
    <target>
        <sequence>
            <property expression="//Data/Token" name="SaveFireToken"
                scope="default" type="STRING" />
            <property expression="//Data/Code" name="SaveCustCode"
                scope="default" type="STRING" />
            <filter regex="(.|\s)*\S(.|\s)*" source="get-property('SaveFireToken')">
                <then />
                <else>
                    <payloadFactory media-type="json">
                        <format>{
                            "OutputTimeStamp": "$1",
                            "OutputRequestID": "$2",
                            "StatusCode" :"3",
                            "StatusMessage" : "No token found"
                            }
                        </format>
                        <args>...
                        </args>
                    </payloadFactory>
                    <respond />
                </else></filter>

                    <script language="js">...</script>
            <payloadFactory description="Request"
                requst to backend
            </payloadFactory>   
             <call>
              <endpoint>
               <address uri="https://..."/>
                 </endpoint>
             </call>
        </sequence>
    </target>
</iterate>

Let's say I've main 2 calls to the backend, then I get 2 responses that look like:

{"id":0000,"success":1,"failure":0,"can_ids":0,"results":[{"message_id":"00000"}]}

I've tried to aggregate them but with no success:

 <aggregate id="it1">
            <completeCondition>
                <messageCount min="-1" max="-1" />
            </completeCondition>
            <onComplete expression="$body/*[1]">
               <log>...<log>
            </onComplete>
        </aggregate>

Can someone show me where I do something wrong? I suspect that I use it wrong- xpression="$body/*[1]", or if there's way to combine the result property in the json from the responses. Also the aggregate should be inside or outside of the iterate mediator, I've seen it both ways?

EDIT:

After a while I ended up using this:

 <aggregate id="it1">
    <completeCondition>
        <messageCount max="-1" min="-1"/>
    </completeCondition>
    <onComplete expression="//jsonObject/success" xmlns:ns="http://org.apache.synapse/xsd">
        <log level="full">...</log>
    </onComplete>
</aggregate>

I don't know if it's the best solution but it worked for me and helped me extract the values that I needed from the response and aggregate them.

Upvotes: 2

Views: 855

Answers (1)

Arunan
Arunan

Reputation: 3426

My first thought when I see this question is whether the expression you have given for the iterate mediator is correct. Assuming you have included the exact json payload you are iterating, the expression should be as follow :

<iterate expression="//Response/data" id="it1">
    .....
</iterate>

For your second question, your expression for aggregate mediator is correct. And you have to have the aggregate mediator outside the iterate mediator. The only issue I see is that you don't have an enclosing element to merge the individual responses. In other words, we need to have an outer array. To achieve please do something like below. (Add an enclosingElementProperty attribute to the onComplete tag)

<property name="Aggregated_Responses" scope="default">
     <jsonObject />
</property>    
<aggregate id="it1">
     <completeCondition>
            <messageCount min="-1" max="-1" />
     </completeCondition>
     <onComplete expression="$body/*[1]" enclosingElementProperty="Aggregated_Responses">
             <log>...<log>
     </onComplete>
</aggregate>

Iterate mediator processes the json payload as xml in the implementation level. Hence we need to have such workarounds to achieve this functionality. For more references please refer to this blog. https://medium.com/@nirothipanram/wso2-esb-iterate-and-aggregate-json-requests-ab5fc3617f

Upvotes: 1

Related Questions