cancel
Showing results for 
Search instead for 
Did you mean: 

JSON to XML error Rest

former_member190536
Participant
0 Kudos

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?

Accepted Solutions (1)

Accepted Solutions (1)

former_member194786
Active Contributor
0 Kudos

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 Kudos

hello Sanjeev,

I checked it,

What I have to provide there?

former_member194786
Active Contributor
0 Kudos

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

Regards,

Sanjeev

anupam_ghosh2
Active Contributor
0 Kudos

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 Kudos

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_member194786
Active Contributor
0 Kudos

Hi Khaja,

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

Regards,

Sanjeev

anupam_ghosh2
Active Contributor
0 Kudos

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

Answers (5)

Answers (5)

former_member190536
Participant
0 Kudos

hi Anupam,

This Java mapping is taking 10 mins of time if I run 1,000 line items, Can we reduce time?

anupam_ghosh2
Active Contributor
0 Kudos

Hi Khaja,

                Delete lines 95 to 99 , recompile the code and run again.

Regards

Anupam

former_member190536
Participant
0 Kudos

yes I deleted those lines and updated the mapping, still it is taking 10min for one msg which having 1000 line items...

anupam_ghosh2
Active Contributor
0 Kudos

Please open a new thread. I am trying to think of alternative algorithm to deal with this issue.

can you please share the payload which is causing the issue.

Regards

Anupam

praveen_vanga3
Participant
0 Kudos

Hi Khaja,

We configured REST adapter as a receiver .It may useful below screen shot for the reference.

Java mapping required but before that get response in CDATA and do modification as per your message type .you may required few lines of code.

Once you see the response in CDATA , you build the logic accordingly.

Thanks

Praveen

anupam_ghosh2
Active Contributor
0 Kudos

Hi Praveen,

                   Issue is with REST sender adapter as per first post by Khaja.

Regards

Anupam

anupam_ghosh2
Active Contributor
0 Kudos

Hi Khaja,

                  Can you pls provide the complete json input and complete xml as required by mapping.

Regards

Anupam

former_member190536
Participant
0 Kudos

My source JSON

"CalculationBaseQuantity": {

"-unitCode": "KG",

"#text": "1"

I want to convert into XML.

REST Adapter is converting into XML as follows..

<CalculationBaseQuantity><unitCode>KG</unitCode><text >1</content></text>

Expected XML in mapping required as follows..

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

Due to this I am getting error in mapping as follows.

So how should my JSON format required to form this correct xml?

apu_das2
Active Contributor
0 Kudos

Hi Khaja,

Define this field as attribute in sender CC Custom XML/JSON conversion table and look how this behave.

Thanks,

Apu

anupam_ghosh2
Active Contributor
0 Kudos

Hi Khaja,

                 Can you please confirm if your source json is exactly shown below

{

  "CalculationBaseQuantity": {

    "-unitCode": "KG",

    "#text": "1"

  }

}

Can you please confirm version of PI and service pack you are using.

Regards

Anupam

former_member190536
Participant
0 Kudos

yes Anupam, it is same as you mentioned above.

{

                            "CalculationBaseQuantity": {

                                "-unitCode": "KG",

                                "#text": "1"

}

tried with multiple cases like  with "@unitCode":"KG",  "$":"1" 

"_unitCode":"KG",  "$":"1" . etc...

in all cases xml is not forming attribute field unitCode  and in mapping getting failed.

I am using PO 4.0.

apu_das2
Active Contributor
0 Kudos

Hi Khaja,

Did you try with the escape character check box in sender REST CC. Try with that. If that solve your issue then its fine otherwise you need to write one custom module to handle this as the error is coming in the CC conversion ... # will not be treated as a valid xml attribute character.

If you need to write custom module exactly follow this and use module parameters in your CC accordingly -

Thanks,

Apu

former_member186851
Active Contributor
0 Kudos

Hello Khaja,

I guess the source JSON is not well formatted.

Tats why getting the error while converting to XML.

https://www.freeformatter.com/json-to-xml-converter.html

former_member190536
Participant
0 Kudos

Ya i am doing from those tools only.

former_member186851
Active Contributor
0 Kudos

Source Has the same JSON?it seems to be wrong.

Might you require a module to convert it in SAP PI.

former_member190536
Participant
0 Kudos

so for this <CalculationBaseQuantity unitCode="KG">1</CalculationBaseQuantity>

how JSON to be expected? then I will correct this JSON and I will send test to PO.

former_member186851
Active Contributor
0 Kudos

Check the below example

JSON Example