Question

My back-end service responses with XML, and I return it as is by default.

I need to convert a response to JSON inside the "outSequence" if a client give me an additions argument (like: &outout_format=json).

For example:

<result>
<foo>bar</foo>
<foo2>bar2</foo2>
<nested>
<node>value</node>
</nested>
</result>

should be responded as

{
"foo": "bar",
"foo2": "bar2",
"nested":{
"node":"value"
}
}

Here is a test proxy-service (I just use here inSequence to show the problem):

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
           name="JSONtest"
           transports="https,http"
           statistics="disable"
           trace="disable"
           startOnLoad="true">
       <target>
          <inSequence>
             <property name="TEST_WAITING_FOR"
                       value="json"
                       scope="default"
                       type="STRING"/>
             <header name="To" action="remove"/>
             <property name="RESPONSE" value="true"/>
             <property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
             <payloadFactory media-type="xml">
                <format>
                   <response xmlns="">
                      <result>success</result>
                      <code>123</code>
                   </response>
                </format>
                <args/>
             </payloadFactory>
             <switch source="get-property('TEST_WAITING_FOR')">
                <case regex="json">
                   <property name="messageType"
                             value="application/json"
                             scope="axis2"
                             type="STRING"/>
                </case>
                <default>
                   <property name="messageType" value="text/xml" scope="axis2" type="STRING"/>
                </default>
             </switch>
             <send/>
          </inSequence>
       </target>
       <description/>

It responses:

{"response":{"result":"success","code":123}}

Is there any way to remove the root-node "response" to make it look like this?

{"result":"success","code":123}

When I try to remove a node "result" with Enrich-mediator (ex. $body/result/* -> $body/*) it becomes an invalid XML with several root-nodes, and JSON contains only the first one.

I know that it's possible to Payload a JSON message, but backend can return an XML with different formats and various number of nested nodes so I cannot hardcode it as a JSON.

It seems that i need to implement my own JSONMessageFromatter? (any example of code?)

UPD: I found the solution (thanks to Dakshika)

<payloadFactory media-type="json">
            <format>$1</format>
            <args>
               <arg expression="$.response" evaluator="json"></arg>
            </args>
</payloadFactory>
Was it helpful?

Solution

With ESB 4.7 release, the payload factory mediator support multiple media types, xml and json and you can change accordingly.

Try below Links

http://www.dilan.me/articles/tech-notes/how-to-use-wso2-products-esb-payload-factory-mediator-to-change-json-service-output/

http://wso2.com/library/articles/2013/12/restful-integration-with-wso2-esb/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top