on 07-28-2016 11:13 AM
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?
Hi Khaja,
Have you tried using this option in REST adapter:
Remove mangling of invalid name characters
Remove escaped name start character
Cheers,
Sanjeev
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.
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
hi Anupam,
This Java mapping is taking 10 mins of time if I run 1,000 line items, Can we reduce time?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Khaja,
Can you pls provide the complete json input and complete xml as required by mapping.
Regards
Anupam
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Khaja,
I guess the source JSON is not well formatted.
Tats why getting the error while converting to XML.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Check the below example
User | Count |
---|---|
81 | |
24 | |
11 | |
9 | |
7 | |
5 | |
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.