Reputation: 378
{"methods":[[{"p_id":"v1","offline":false,}],[{"p_id":"v2","offline":true,}]]}
The above is my JSON payload. I am making use of nested Iterate mediators to fetch the values of p_id and v1. I am able to fetch the values successfully, but the logs outside the first iterate mediator are not getting printed.
Following is my code:
<iterate expression="json-eval($.methods)" id="iterate-over-Methods">
<target>
<sequence>
<iterate expression="json-eval($)" id="2nd-iteration">
<target>
<sequence>
<filter regex="false" source="json-eval($.offline)">
<then>
<property expression="json-eval($.p_id)" name="p-id" scope="default" type="STRING"/>
<log>
<property expression="$ctx:p-id" name="p-id-set"/>
</log>
</then>
<else>
<log>
<property name="p-id-set" value="Provider ID already set"/>
</log>
</else>
</filter>
<log>
<property name="status-1" value="Inside 1st iteration" />
</log>
</sequence>
</target>
</iterate>
<log>
<property name="status-2" value="Inside 2nd iteration" />
</log>
</sequence>
</target>
</iterate>
Is the code going into some infinite loop that it is not coming out? How can I handle this? Thanks in advance..
Upvotes: 2
Views: 440
Reputation: 14574
From the information you provided I think you can use a single Iterator here.(But this really depends on your full usecase). Here is a sample with a single Iterate Mediator.
<iterate expression="json-eval($.methods)" id="pl-iterator">
<target>
<sequence>
<property expression="json-eval($[0].p_id)" name="p-id" scope="default" type="STRING"/>
<log level="full">
<property expression="$ctx:p-id" name="p-id-set"/>
</log>
</sequence>
</target>
</iterate>
Also, an Iterate Mediator
should be coupled with an Aggregate Mediator
. So make sure you add appropriate aggregation logic as well.
Iterate mediator should not be considered as a for-loop in programming languages. When you Iterate over a payload, the Payload will be split and each segment will concurrently execute on its own thread. So having nested Iterate Mediators is not a good idea unless it's really necessary.
Upvotes: 3