cancel
Showing results for 
Search instead for 
Did you mean: 

Adapter module or Java mapping to convert object into an array in JSON output

harish_babu5
Participant
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

engswee
Active Contributor
0 Kudos

You are welcome to try my updated module which can handle forcing all or certain fields to be created as JSON arrays.

harish_babu5
Participant
0 Kudos

HI Eng

For a start, I am using a Java mapping to convert xml to JSON.

This module can be used after using Java mapping?

Harish

Answers (2)

Answers (2)

harish_babu5
Participant
0 Kudos

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

former_member182412
Active Contributor
0 Kudos

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.

harish_babu5
Participant
0 Kudos

Thanks Praveen

Hope this takes care of the cases when "Items" node comes as an array too.

Anyway, I will try it and update you

Regards

Harish Babu

harish_babu5
Participant
0 Kudos

HI Praveen

I am getting this error.

I am not able to import the jar file org.apache.commons.collections.jar to the imported archives. It's throwing me error and I have included that as a screen shot.

If I don't import that jar file, I am getting an error

former_member182412
Active Contributor
0 Kudos

Hi Harish,

I also get the same error but i did not import the collections jar into repository still it works for me. import all other jars except collections and try?

Regards,

Praveen.

harish_babu5
Participant
0 Kudos

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.

harish_babu5
Participant
0 Kudos

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

former_member182412
Active Contributor
0 Kudos

Hi Harish,

Can you give me your XML payload which you are testing??

Regards,

Praveen.

harish_babu5
Participant
0 Kudos

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

former_member182412
Active Contributor
0 Kudos

Hi Harish,

Did you check this blog your case is already covered in this blog.

You just need to mention the items property arrayType to true.

Regards,

Praveen.

harish_babu5
Participant
0 Kudos

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

former_member182412
Active Contributor
0 Kudos

Hi Harish,

Can you provide java mapping then we can adjust the java mapping for this requirement also.

Regards,

Praveen.

harish_babu5
Participant
0 Kudos

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

nitindeshpande
Active Contributor
0 Kudos

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