DaviesTobi alex
DaviesTobi alex

Reputation: 670

Enrich Payload in WSO2

I have a JSON array of the following structure.

 {"paymentItems": [
    {
        "amount": "180000",
        "code": "28"
    },
    {
        "amount": "396000",
        "code": "06"
    },
    {
        "amount": "1460000",
        "code": "01"
    }
]
}

Am trying to enrich each item in the array list with an additional JSON value.

<foreach expression="//paymentItems" id="1">
            <sequence>
                <property expression="//paymentItems/amount" name="amount" scope="default" type="STRING"/>
                <property expression="//paymentItems" name="body" scope="default" type="STRING"/>
                <log>
                    <property expression="$ctx:amount" name="INIDIVIDUAL_AMOUNT"/>
                </log>
                <script language="js"><![CDATA[var amount = mc.getProperty('amount'); var naira = amount/100; mc.setProperty("nairaValue", naira);]]></script>
                <log>
                    <property expression="get-property('nairaValue')" name="NAIRA_VAL"/>
                </log>
                <property expression="get-property('nairaValue')" name="naira" scope="default" type="STRING"/>
                <enrich>
                    <source type="custom" xpath="$ctx:nairaValue"/>
                    <target action="child" type="body"/>
                </enrich>
            </sequence>
 </foreach>

As you can see I process the value in the foreach and then use the result and try to add to the array item but it throws no errors and does not add the value.

Upvotes: 0

Views: 324

Answers (1)

Lahiru Madushanka
Lahiru Madushanka

Reputation: 254

Foreach mediator does the following

  1. first take a clone of the original message
  2. take an iterated element from the original message (using XPath)
  3. Create a new message context by adding the iterated element to the cloned envelope
  4. Do the mediation steps given in the sequence for that new message context

Since for each iteration, we are cloning a new message context (say context2), and the original message context(say context1) is a separate one, we cannot enrich from context2 to context1.

That's the reason for the behaviour you are experiencing.

As a remedy, you can do the iteration itself from the script mediator and alter the message as required.

Upvotes: 1

Related Questions