Fadli Nurul
Fadli Nurul

Reputation: 25

WSO2 - How to convert this XML to Json using script mediator?

Im using WSO2 ESB and DDS to make an API to read data from database and finally I got this respone. How to convert this XML to Json using script mediator? Give me an example please.

<?xml version='1.0' encoding='UTF-8'?> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
    <employeeCollection xmlns="http://employee.us.wso2.com">
        <employee>
            <EmployeeID>17</EmployeeID>
            <FirstName>jak</FirstName>
            <LastName>123</LastName>
            <Team>ok</Team>
        </employee>
        <employee>
            <EmployeeID>18</EmployeeID>
            <FirstName>jak</FirstName>
            <LastName>123</LastName>
            <Team>ok</Team>
        </employee>
        <employee>
            <EmployeeID>19</EmployeeID>
            <FirstName>jak</FirstName>
            <LastName>123</LastName>
            <Team>ok</Team>
        </employee>

    </employeeCollection>
</soapenv:Body>

And this is the configuration

<resource methods="POST" uri-template="/team">
  <inSequence>
     <sequence key="conf:/SendSelectWithTeam"/>
     <call>
        <endpoint>
           <address uri="https://192.168.2.165:9453/services/EmployeesDataService/" format="soap12"/>
        </endpoint>
     </call>

     <respond/>
  </inSequence>

Additional question: How to get the value of the each of "EmployeeID" using script mediator?

Upvotes: 0

Views: 1680

Answers (2)

Ponmani Chinnaswamy
Ponmani Chinnaswamy

Reputation: 865

Refer this link

How to convert SOAP response with xsi values to json in WSO2esb

Please change the proxy outSequence settings in config file like below:

  <outSequence xmlns="http://ws.apache.org/ns/synapse">
   <property name="messageType" value="application/json/badgerfish" scope="axis2" type="STRING"></property>
   <send></send>
</outSequence>

Upvotes: 0

Bee
Bee

Reputation: 12502

If you just want to convert this directly to json, you don't need to do it manually using script mediator.

You can update the out sequence of your ESB proxy (or API) like this and it will convert the response xml to json.

<outSequence>
     <property name="messageType" value="application/json" scope="axis2"/>
     <send/>
</outSequence>

See WSO2 Docs for more details.

Edit:

In your case, if you use <respond> mediator, it will bypass out sequence, and my suggestion won't work. You have 2 options to get it working.

1) Use Send mediator instead of call and respond mediators.

or

2) Use loopback mediator instead of respond mediator.

Upvotes: 1

Related Questions