Using the WSO2 Enterprise Integrator 6.4.0 for creating web services. Web service structure briefly;
Request comes to the API.xml
In Sequence.xml taking JSON payload with;
<property expression="json-eval($.)" name="payload" scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
- Giving the JSON payload to the Transformation.xslt file as payload;
<xslt key="conf:xsl/RequestTransform.xsl" source="$body[1]/*" xmlns:ns="http://org.apache.synapse/xsd"/>
- Sending the payload to the endpoint as JSON. Result of the transformation is XML payload, changing the type to JSON with PayloadFactory.
<payloadFactory media-type="json">
<format>$1</format>
<args>
<arg evaluator="xml" expression="$body[1]/*" literal="false" xmlns:ns="http://org.apache.synapse/xsd"/>
</args>
</payloadFactory>
- Taking the response from endpoint in JSON format. Receiving the payload with '@' chars. This chars means that it is an attribute. Ex:
{
"address": "1590 Lar",
"@country": "SMCountry123",
"language": "SMLanguage123"
}
Here is the problem I have that I need to remove '@' char from my response JSON payload which is came from external system. And need to transform response with .xslt file after removing '@'.
My first try was Script Mediator;
<script language="js">
<![CDATA[var payload = mc.getProperty("payload");
payload=String(payload).replace('@'/gm,' ');
payload = JSON.parse(payload);
mc.setPayloadJSON(payload);]]>
</script>
but recieving the errors
javax.xml.stream.XMLStreamException: Cannot read attribute: element has children or text and Existing json payload is malformed.
Then I try to use Class Mediator prepare a .jar for WSO2 lib for;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;
public class ChangeAtType extends AbstractMediator {
private String payload = "payload";
public boolean mediate(MessageContext context) {
String payload = (String)context.getProperty(this.payload);
payload = payload.replace("@","");
context.setProperty(this.payload, payload);
return true;
}
}
and called it from sequence;
<property expression="json-eval($.)" name="payload" scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
<class name="org.com.ChangeAtType.ChangeAtType"/>
<property expression="get-property('payload')" name="payload" scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
but still same errors I have.
Could you please advise? Thanks