cancel
Showing results for 
Search instead for 
Did you mean: 

JAVA mapping for JSON object

Former Member
0 Kudos

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......

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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:

http://stackoverflow.com/questions/19586199/aws-json-exception-a-jsonobject-text-must-begin-with-at-...

Regards,

Pierre.

Former Member
0 Kudos

Hi Pierre,

Thanks..

Yes I am using the same payload which I received after running it from SOAP UI by firing the URL. It works in Operation Mapping but fails when I initiate from ABAP Proxy to JSON service.

I tried the above one suggested its not working...

Regards...

Former Member
0 Kudos

Hi Rebecca,

What do you mean by "it is not working"? Do System.out.println((int)sourcexml.charAt(0)); return 123?

If not it is the reception of the payload which is the issue because sourcexml must start with a curly bracket.

Please tell us what "int" you get.

Regards,
Pierre.

Answers (2)

Answers (2)

former_member629447
Participant
0 Kudos

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.

former_member184720
Active Contributor
0 Kudos

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

java - A JSONObject text must begin with &amp;#39;{&amp;#39; at 1 [character 2 line 1] with &amp;#39...

May be you are also receiving the JSON response in "[" "]"

If so you might want to replace those braces as suggested by akb.