cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

JSON to XML error Rest

former_member190536
Participant
0 Likes
3,976

Hello,

I am using REST Adapter to receive JSON format message in PO.

the normal XML work for me, but when we have ATTRIBUTES in our strucutre then the json to xml conversion error is coming due to this in mapping the message gets failed.

My target xml strucutre like this.

<CalculationBaseQuantity unitCode="KG">1</CalculationBaseQuantity>

in my source JSON  it is coming like this..

                            "CalculationBaseQuantity": {

                                "-unitCode": "KG",

                                "#text": "1"

After Seder adapter the message come in mapping as ...

<CalculationBaseQuantity><-unitCode>KG</-unitCode><#text>1</#text></CalculationBaseQuantity>

due to this mapping getting failed as xml formatting issues.

error in mapping - The content of elements must consist of well-formed character data or markup., BaseRuntimeException: The content of elements must consist of well-formed character data or markup., BaseRuntimeException: The content of elements must consist of well-formed character data or markup.,

does standard REST adapter wont support this functionality? does i need to go for Custom adapter only?

View Entire Topic
Former Member
0 Likes

Hi Khaja,

Have you tried using this option in REST adapter:

Remove mangling of invalid name characters

Remove escaped name start character


Cheers,

Sanjeev

former_member190536
Participant
0 Likes

hello Sanjeev,

I checked it,

What I have to provide there?

Former Member
0 Likes

Try with the special characters that you are getting in field name, such as "-".

Regards,

Sanjeev

anupam_ghosh2
Active Contributor
0 Likes

Hi Khaja,

             

Please let us know in case the solution provided by Sanjeev was able to resolve the issue.

here is the updated java mapping to resolve the issue.

This needs to be applied in same OM before message mappings


import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.xml.sax.InputSource;

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 JsonToxml  extends AbstractTransformation{

  /**

  * @param args

  * @throws FileNotFoundException

  * @throws StreamTransformationException

  */

  StringBuilder s=new StringBuilder("");

  BufferedReader reader;

 

  

  @Override

  public void transform(TransformationInput arg0, TransformationOutput arg1)

  throws StreamTransformationException {

  // TODO Auto-generated method stub

  execute(arg0.getInputPayload().getInputStream(),arg1.getOutputPayload().getOutputStream());

  }

  public void adjustAttribute(char c) throws StreamTransformationException

  {

  /******

  * this function removes all text nodes and attribute nodes from payload

  */

  try

  {

  String t="><"+c;

  String attributeName="",attributeValue="";

  int p=s.indexOf(t);

  if(p>=0)

  {

  //attribute present

  int y=s.indexOf(">",p+1);

  attributeName=s.substring(p+3,y);

  int x=s.indexOf("<",y+1);

  attributeValue=s.substring(y+1,x);

  int z=s.indexOf(">", x+1);

  if(c=='-')

  {

  s=new StringBuilder(s.substring(0,p)+" "+attributeName+"=\""+attributeValue+"\">"+s.substring(z+1,s.length()));

  }

  else if(c=='#')

  {

  if(attributeName.equalsIgnoreCase("text"))

  {

  s=new StringBuilder(s.substring(0,p+1)+attributeValue+s.substring(z+1,s.length()));

  }

  }

  }

  }

  catch(Exception e)

  {

  e.printStackTrace();

    throw new StreamTransformationException(e.getMessage());

  }

  return;

  }

    public void execute(InputStream in, OutputStream out) throws StreamTransformationException

    {

    try

    {

  

    boolean status=false;

    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    int c;

    while((c=reader.read())!=-1)

    {

    s.append((char)c);

    }

    reader.close();

    System.out.println(s);

    String symbol[]={"-","#"};

    for(int i=0;i<symbol.length;++i)

    {

    while(s.indexOf("<"+symbol[i])>=0)

    {

    adjustAttribute(symbol[i].charAt(0));

    }

    }

    System.out.println(s);

    //check if the xml is well formed

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 

            DocumentBuilder builder; 

            builder = factory.newDocumentBuilder(); 

            Document doc = builder.parse( new InputSource( new StringReader( s.toString() ) ) );

            System.out.println("xml well formed");

            out.write(s.toString().getBytes("UTF-8"));

    }

    catch(Exception e)

    {

    e.printStackTrace();

    throw new StreamTransformationException(e.getMessage());

    }

    }

}

Input


<a><CalculationBaseQuantity><-unitCode>KG</-unitCode><-FunitCode>KG1</-FunitCode><#text>1</#text></CalculationBaseQuantity><m>2</m></a>

Output

<a><CalculationBaseQuantity unitCode="KG" FunitCode="KG1">1</CalculationBaseQuantity><m>2</m></a>



Regards

Anupam

former_member190536
Participant
0 Likes

Hi Anupam,

This JAVA code working fine for me.

1. But I don't have external JAR files with me to create JAVA Mapping, so I did directly used this code in Graphical mapping in "Funcitons" tab. It is working fine. But in design time test using  "Test" tab then it is hanging and not responding. Do you have information abt tht?

2. How to create Java mapping directly in NWDS directly? (means with out import archives option)

@Sanveev: Thanks for your response, I used those 2 fields in Sender REST Adapter, but it is not working.

I checked Escape invalid name set character and Mangle invalid character set and used Escape sequence "-"

then it is creating "--" characters in mapping.

Former Member
0 Likes

Hi Khaja,

Apologies, my bad. I read the documentation the other way round.

Regards,

Sanjeev

anupam_ghosh2
Active Contributor
0 Likes

Hi Khaja,

              The java mapping requires no external jar files. I have compiled and converted the code into jar file.

Please download this attachment and upload in the OM as first mapping followed by graphical mapping.

rename the attachment to "jsonToXml.jar " after downloading the same in your local system. Then upload the same in PI server.

If this is still giving issues. Please provide the test payload to graphical mapping being used in the OM.

The error might be due to the fact that output of java mapping is not matching with expected input to following graphical mappings.

Regards

Anupam