[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"
         <property name="messageType" value="application/json" scope="axis2"/>

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.

        <name>Bermuda Triangle</name>
        <name>Eiffel Tower</name>

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