on 10-15-2014 10:55 AM
Dear Experts,
I am trying to convert JSON data to XML with the below code which was shared by Hareesh. It works
fine when worked in ESR Operation Mapping Test tab. But when the same payload is run during runtime , it shows error.
Code
package com.sap.json;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.json.JSONObject;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class ConvJsonXml extends AbstractTransformation {
public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {
try {
String sourcexml = ""; String targetxml =""; String line ="";
InputStream ins = in.getInputPayload().getInputStream();
BufferedReader br = new BufferedReader( new InputStreamReader(ins));
while ((line = br.readLine()) != null)
sourcexml +=line+"\n";
br.close();
JSONObject o = new JSONObject(sourcexml);
targetxml = org.json.XML.toString(o);
targetxml = "<root>"+targetxml+"</root>";
out.getOutputPayload().getOutputStream().write(targetxml.getBytes());
}
catch (Exception e) {
throw new StreamTransformationException(e.getMessage());
}
}
}
Payload
{
"items": [
{
"item": {
"Subject": "test by DS Ross",
"DocumentID": "55DCE0A476D",
"Sender": "Rebecca",
"SenderName": "user1",
"Date": "2013-02-14 11:14:40",
"ClassID": "11"
}
},
{
"item": {
"Subject": "test by Nick M",
"DocumentID": "55DCE191D5E47",
"Sender": "Dave Froster",
"SenderName": "user11",
"Date": "2013-03-14 11:14:40",
"ClassID": "11"
}
}
]
}
When tested in Operation Mapping Test tab, it shows no error. But when the same payload is received from Server, it shows the Mapping error
<SAP:Stack> com.sap.engine.interfaces.messaging.api.exception.MessagingException: com.sap.engine.interfaces.messaging.api.exception.MessagingException: Error encountered while executing mapping: com.sap.aii.af.service.mapping.MappingException: Application mapping program com/sap/json/ConvJsonXml throws a stream transformation exception: A JSONObject text must begin with '{' at 1 [character 2 line 1] at com.sap.aii.adapter.soap.web.SOAPHandler.processSOAPtoXMB(SOAPHandler.java:746) at com.sap.aii.adapter.soap.web.MessageServlet.doPost(MessageServlet.java:505) at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at com.sap.engine.services.servlets_jsp.server.runtime.FilterChainImpl.runServlet(FilterChainImpl.java:202) at
I would appreciate the help shared..
Regards......
Hi Rebecca,
Are you sure to receive the exact same payload you described above from the server?
You can try:
System.out.println((int)sourcexml.charAt(0));
Which has to return 123. 123 means it does starts with curly bracket.
This thread on stackoverflow.com may also help you:
Regards,
Pierre.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I came across this problem as well and below was the solution (in case somebody may encounter):
char a = '{';
char b = '}';
sourcexml = sourcexml.substring(sourcexml.indexOf(a), sourcexml.lastIndexOf(b) + 1);
JSONObject o = new JSONObject(sourcexml);
Just before the JSONObject instance, add that statement. This statement will look for the first index for '{' and the last index for '}' and keep the sub string. Add 1 is needed to keep the closing curly bracket.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Looking at the error i don't think JSON response is same as what you are testing in ESR.
Have a look at the below therad
May be you are also receiving the JSON response in "[" "]"
If so you might want to replace those braces as suggested by akb.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
25 | |
12 | |
9 | |
6 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.