on 05-26-2016 1:25 PM
Hi
I am on single stack SAP PO. Mine is a REST to RFC synchronous call. I am using Java mapping to convert the xml to JSON. The ECC system is sending a URL in the response message. If I use the conversion of REST adapter, the forward slashes in the URL are escaped with a backward slash.
So I have to use Java mapping.
Because of that, I cannot use the SAP's method to convert xml into JSON.
In my target payload, there is a node called "Item".
"Items" : [
{
"Quantity": 1,
"UnitOfMeasure": "EA",
"MaterialDescription": "ABCD"
}
{
"Quantity": 2,
"UnitOfMeasure": "FR",
"MaterialDescription": "FGHI"
}
]
This appears an array!
If there is only one set of values, then Items appear as object
"Items" : {
{
"Quantity": 1,
"UnitOfMeasure": "EA",
"MaterialDescription": "ABCD"
}
}
I want the node "Items" to appear as array always. If it is an array then let it be array, and if it is an object it should be an array.
Is there a way to achieve this using existing Adapter module or through a Java Mapping?
Regards
Harish Babu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
HI all
The reason I used Java mapping to convert xml to JSON is because when I use the conversion of REST adapter, then I am getting backward slashes when I send uRLs in payload.
Is there any fix provided by the SAP for this bug in the REST adapter?
If so, I can use that patch and go for the normal conversion by REST adapter and use adapter module mentioned by various posters here to take care of the object-array issue
Regards
Harish
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Harish,
Use below java mapping.
import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;
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 XMLToJSONJavaMap extends AbstractTransformation {
@Override
public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)
throws StreamTransformationException {
try {
JSON objJson = new XMLSerializer().readFromStream(transformationInput.getInputPayload().getInputStream());
transformationOutput.getOutputPayload().getOutputStream().write(objJson.toString().getBytes());
} catch (Exception e) {
getTrace().addDebugMessage("Exception while writing OutputPayload: IOException", e);
throw new StreamTransformationException(e.toString());
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item>
<Quantity>1</Quantity>
<UnitOfMeasure>EA</UnitOfMeasure>
<MaterialDescription>ABCD</MaterialDescription>
</Item>
</Items>
[{
"Quantity": "1",
"UnitOfMeasure": "EA",
"MaterialDescription": "ABCD"
}]
You need to import all these jars as imported archives.
Jars for above imported archive are below.
Regards,
Praveen.
Hi Praveen
I am getting an error in runtime.
NoClassDefFoundError
I have used that collections jar in NWDS. I did not get any error there. I exported the same mapping as jar file to my machine and then imported it as jar into esr.
I think if we use a jar file in NWDS, we have to use it in ESR too. Should I remove that jar from there and export again.
Hi Praveen
Unfortunately, the code is not working
I am not getting the node coming as Array with square brackets. It is coming as object only.
See the screen shots of the code and payload. Payload was taken from Postman
First time, multiple Items are there 10 and 11, so it comes as an array.
Next time, only one item 0019 is there, so it comes as object with curly braces
Regards
Harish
HI
This is the xml payload
<ns1:MT_MaterialMaster_ABC_Response_Receiver xmlns:ns1="urn:XYZ">
<MaterialMaster>
<MaterialNumber>000000000000100001</MaterialNumber>
<MaterialName>100001</MaterialName>
<MaterialDescription>TEST 123</MaterialDescription>
<MaterialGroup>Perishable</MaterialGroup>
<ProductCategory>000000000010000554</ProductCategory>
<PeriodicUnitPrice>0</PeriodicUnitPrice>
<DomesticSellingPrice>5.00</DomesticSellingPrice>
<InternationalSellingPrice>12.00</InternationalSellingPrice>
<Items>
<Material_Number>000000000000100010</Material_Number>
<Material_Description>TEST 123</Material_Description>
<Quantity>1.000</Quantity>
<UnitOfMeasure>EA</UnitOfMeasure>
</Items>
<Items>
<Material_Number>000000000000100011</Material_Number>
<Material_Description>TEST 123</Material_Description>
<Quantity>1.000</Quantity>
<UnitOfMeasure>EA</UnitOfMeasure>
</Items>
<links>
<self>
<href>http://host:port/RESTAdapter/Materials/100001</href>
</self>
</links>
</MaterialMaster>
<MaterialMaster>
<MaterialNumber>000000000000100002</MaterialNumber>
<MaterialName>DSC-111</MaterialName>
<MaterialDescription>TEST 123-1</MaterialDescription>
<MaterialGroup/>
<ProductCategory/>
<PeriodicUnitPrice>0</PeriodicUnitPrice>
<DomesticSellingPrice>0</DomesticSellingPrice>
<InternationalSellingPrice>0</InternationalSellingPrice>
<Items>
<Material_Number>000000000000100019</Material_Number>
<Material_Description>Beverages</Material_Description>
<Quantity>1.000</Quantity>
<UnitOfMeasure>EA</UnitOfMeasure>
</Items>
<links>
<self>
<href>http://host:port/RESTAdapter/Materials/100002</href>
</self>
</links>
</MaterialMaster>
<MaterialMaster>
<MaterialNumber>000000000000100003</MaterialNumber>
<MaterialName>100003</MaterialName>
<MaterialDescription>COFEE CAKE 1</MaterialDescription>
<MaterialGroup>Perishable</MaterialGroup>
<ProductCategory>000000000010000455</ProductCategory>
<PeriodicUnitPrice>0</PeriodicUnitPrice>
<DomesticSellingPrice>0</DomesticSellingPrice>
<InternationalSellingPrice>0</InternationalSellingPrice>
<Items>
<Material_Number>000000000000100025</Material_Number>
<Material_Description>Excess Baggage 3 KG</Material_Description>
<Quantity>1.000</Quantity>
<UnitOfMeasure>EA</UnitOfMeasure>
</Items>
<links>
<self>
<href>http://host:port/RESTAdapter/Materials/100003</href>
</self>
</links>
</MaterialMaster>
</ns1:MT_MaterialMaster_ABC_Response_Receiver>
Regards
Harish Babu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Praveen
I myself have used that blog to solve the issue in one integration scenario.
The problem here is, I am using Java mapping to convert xml to JSON. So I cannot go for this method in blog as the payload is already converted to JSON using Java mapping. The reason I used Java mapping is because I was getting my forward slashes escaped with backward slashes.
Now, I have only one way ahead!
Use Java mapping or adapter module to convert object into array
Harish
Hi Praveen
I am using this mapping. In fact, you only provided that.
http://scn.sap.com/thread/3849042
This is the mapping
Regards
Harish
Hello Harish,
Can you go through the code provided by Christian in the below thread? I think so this will be helpful to you.
https://scn.sap.com/thread/3740024
This will be the method to be as Custom Adapter Module.
Regards,
Nitin
User | Count |
---|---|
94 | |
11 | |
11 | |
10 | |
9 | |
8 | |
6 | |
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.