[WSO2 ESB] - Convert XML To JSON

WSO2 ESB supports JSON. The synapse's internal format is always soap. Thus when you expect a JSON response you should set the expected message type. Then relevant message formatters will be hit and convert the message to the requested format. Converting to JSON is only one of them.

The property in action here is below.

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




Creating a sample proxy service 


Create a proxy service whose source code is as below. 

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="ToJSON"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="messageType" value="application/json" scope="axis2"/>
         <respond/>
      </inSequence>
   </target>
   <description/>
</proxy>


Test the Proxy Service

Try the proxy with following. 

curl -v -X POST -H "Content-Type:application/xml" -d@request1.xml "http://localhost:8280/services/tojson"

where your request1.xml is like below.

 <coordinates>
    <location>
        <name>Bermuda Triangle</name>
        <n>25.0000</n>
        <w>71.0000</w>
    </location>
    <location>
        <name>Eiffel Tower</name>
        <n>48.8582</n>
        <e>2.2945</e>
    </location>
 </coordinates>

You will get the response in JSON format. For advance mediation options with JSON please refer "JSON Support - WSO2 ESB".

Hasitha Hiranya

No comments:

Post a Comment

Instagram