I could transform the complete JSON payload with following steps in ESB 4.5.0. These steps involve changing the message builder and message formatter for the application/json content type.
Change the message builder, formatter for JSON; In CARBON_HOME/repository/conf/axis2/axis2.xml file disengage the default message builder and message formatter by commenting out following lines,
<messageBuilder contentType="application/json" class="org.apache.axis2.json.JSONBuilder"/>
<messageFormatter contentType="application/json" class="org.apache.axis2.json.JSONMessageFormatter"/>
Engage JSONStreamBuilder and JSONStreamFormatter by uncommenting following lines,
<messageFormatter contentType="application/json" class="org.apache.axis2.json.JSONStreamFormatter"/>
<messageBuilder contentType="application/json" class="org.apache.axis2.json.JSONStreamBuilder"/>
Write a Javascript function to transform and build the new XML payload.
function transformRequest(mc) {
var array = mc.getPayloadJSON();
var payload = <games/>;
for (i = 0; i < array.length; ++i) {
var elem = array[i];
payload.game += <game>
<position>{elem.position}</position>
<team_id>{elem.team_id}</team_id>
<team>{elem.team}</team>
<played>{elem.played}</played>
<won>{elem.won}</won>
<drawn>{elem.drawn}</drawn>
<lost>{elem.lost}</lost>
<for>{elem["for"]}</for>
<against>{elem.against}</against>
<difference>{elem.difference}</difference>
<points>{elem.points}</points>
<info>{elem.info}</info>
</game>
}
mc.setPayloadXML(payload);
}
Modify the outSequence to execute the transformation on the incoming JSON payload.
<outSequence>
<script language="js" key="conf:/repository/esb/scripts/transformrequest.js" function="transformRequest"/>
<property name="messageType" value="text/xml" scope="axis2"/>
<send/>
</outSequence>