My back-end service responses are with XML, and I return them by default.
I need to convert the response to JSON inside "outSequence" if the client gave me an additional argument (for example: & outout_format = json).
For instance:
<result>
<foo>bar</foo>
<foo2>bar2</foo2>
<nested>
<node>value</node>
</nested>
</result>
should be answered as
{
"foo": "bar",
"foo2": "bar2",
"nested":{
"node":"value"
}
}
Here is a test proxy service (I just use inSequence here 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/>
Answers:
{"response":{"result":"success","code":123}}
Is there a way to remove the root-node "response so that it looks like this:
{"result":"success","code":123}
When I try to delete the node result using Enrich-mediator (e.g. $ body / result / * → $ body / *), it becomes invalid XML with multiple root nodes, and JSON contains only the first.
, Payload JSON, XML , JSON.
, JSONMessageFromatter? ( ?)
UPD: ( Dakshika)
<payloadFactory media-type="json">
<format>$1</format>
<args>
<arg expression="$.response" evaluator="json"></arg>
</args>
</payloadFactory>