Min Naing Oo
Min Naing Oo

Reputation: 1095

How to transform SOAP to JSON array in wso2 ESB using script mediator?

I'm trying to transform the soap request coming from our clients to the REST service we have using wso2 script mediator.

Input (SOAP):

<tran:Request xmlns:tran="http://schemas.demo.com/tran.xsd" xmlns:sub="http://schemas.demo.com/admin.xsd" ...>
    <tran:Specific>
        <tran:Subject>
            <sub:Transaction>
                <sub:postingIndicator>false</sub:postingIndicator>
                <sub:referenceId>23645723</sub:referenceId>
                <sub:entries>
                    <sub:entry>
                        <sub:accountNumber>AC0123456</sub:accountNumber>
                        <sub:credit>true</sub:credit>
                        <sub:amount>
                            <sub:amountValue>20</sub:amountValue>
                            <sub:currencySymbol>USD</sub:currencySymbol>                                
                        </sub:amount>
                    </sub:entry>
                    <sub:entry>
                        <sub:accountId>AC987654</sub:accountId>
                        <sub:credit>false</sub:credit>
                        <sub:amount>
                            <sub:amountValue>20</sub:amountValue>
                            <sub:currencySymbol>USD</sub:currencySymbol>                                
                        </sub:amount>
                    </sub:entry>
                    <sub:entry> .....
                </sub:entries>
            </sub:Transaction>
        </tran:Subject>
    </tran:Specific>
</tran:Request>

Expected output (JSON):

{
    "indicator": false,
    "reference": 23645723,
    "postingEntries": [
        {
            "accountId": "AC0123456",
            "credit": "true",
            "amount": {
                "value": "20",
                "currency": "USD"
            }               
        },
        {
            "accountId": "AC987654",
            "credit": "false",
            "amount": {
                "value": "20",
                "currency": "USD"
            }               
        },
        .....
    ]
}

So far I was able to map the "non-array" xml elements into JSON but stuck at iterating entries "xml array". This is the script mediator I have now.

<sequence name="soap-to-rest" xmlns="http://ws.apache.org/ns/synapse">
    <script language="js">
        <![CDATA[
            var log = mc.getServiceLog();
            var payload = mc.getPayloadXML();
            log.info(payload);

            var adminns = new Namespace('http://schemas.demo.com/admin.xsd');

            // How to iterate the <sub:entries> and fill this array up? <===========
            var postingEntries[];

            mc.setPayloadJSON(
                {
                    "indicator": mc.getPayloadXML()..adminns::postingIndicator.toString(),
                    "reference": mc.getPayloadXML()..adminns::referenceId.toString(),
                    "postingEntries": postingEntries
                }
            );
        ]]>
    </script>
</sequence>

How do I populate the postingEntries json array before setting it in mc.setPayloadJSON()?

Upvotes: 0

Views: 552

Answers (1)

Dilan Premarathna
Dilan Premarathna

Reputation: 1294

It would be easier to use the data mapper mediator for your use case. Using the integration studio you can easily map the inputs and the outputs. Refer to the document [1] for this.

[1]-https://ei.docs.wso2.com/en/7.2.0/micro-integrator/references/mediators/data-Mapper-Mediator/

Upvotes: 1

Related Questions