Converting XML to JSON using GSON in PI Java Mapping

13k views Asked by At

I am facing issue while converting the source XML into JSON using the GSON library. Please find below the source XML, code, actual output and expected output. I am implementing this in Java mapping of SAP PI where I am getting source XML as input to this Java code.

Source XML:

<data>
<ERP>0080001</ERP>
<Shiptoparty>0088000</Shiptoparty>
<Shippingpoint>503</Shippingpoint>
<Issuedate>20181102</Issuedate>
<products>
<Unit>L</Unit>
<QTY>2.000 </QTY>
<SKUno>000000000011</SKUno>
</products>
<products>
<Unit>L</Unit>
<QTY>0.000 </QTY>
<SKUno>000000000011000078</SKUno>
</products>
</data>

Code:

package xmlgsonjson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
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 MakeItJSONByGSON extends AbstractTransformation 
{

public String targetfile ="";

public MakeItJSONByGSON() {}

public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException
{
try {
    String sourcexml = "";
    
    String line = "";

    
    InputStream inputstream = in.getInputPayload().getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(inputstream));
    OutputStream outputstream = out.getOutputPayload().getOutputStream();
   
    byte[] b = null;
    try {
        b = new byte[inputstream.available()];
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        inputstream.read(b);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String inputContent = new String(b);
 
    
    
    
    sourcexml = inputContent;



    try {
        while ((line = br.readLine()) != null) {
            
        sourcexml = sourcexml + line;
    
        
        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    
    String jsonPrettyPrintString = gson.toJson(sourcexml);
     
 
    targetfile = jsonPrettyPrintString;
    

out.getOutputPayload().getOutputStream().write(targetfile.getBytes());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

Output:

"\u003cdata\u003e\n    \u003cERP\u003e0080001\u003c/ERP\u003e\n    \u003cShiptoparty\u003e0088000\u003c/Shiptoparty\u003e\n    \u003cShippingpoint\u003e503\u003c/Shippingpoint\u003e\n    \u003cIssuedate\u003e20181102\u003c/Issuedate\u003e\n    \u003cproducts\u003e\n    \u003cUnit\u003eL\u003c/Unit\u003e\n    \u003cQTY\u003e2.000 \u003c/QTY\u003e\n    \u003cSKUno\u003e000000000011\u003c/SKUno\u003e\n    \u003c/products\u003e\n    \u003cproducts\u003e\n    \u003cUnit\u003eL\u003c/Unit\u003e\n    \u003cQTY\u003e0.000 \u003c/QTY\u003e\n    \u003cSKUno\u003e000000000011000078\u003c/SKUno\u003e\n    \u003c/products\u003e\n    \u003c/data\u003e"

Expected output:

{"data": [
    {
        "ERP": 0080001,
        "Issuedate": 20181102,
        "Shippingpoint": 503,
        "Shiptoparty": 0088000,
        "products": [
            {
                "Unit": "L"
                "QTY": 2.000,
                "SKUno": 000000000011,   
            },
            {
                "Unit": "L"
                "QTY": 0.000,
                "SKUno": 000000000011000078,
                
            }
        ]
    }
    
]}

Kindly let me know how to resolve this issue.

1

There are 1 answers

7
ChillyWe On

I think you have problem with encoding. Possible solution is

String jsonString = new Gson().toJson(objectToEncode);
byte[] utf8JsonString = jsonString.getBytes("UTF8");
responseToClient.write(utf8JsonString, 0, utf8JsonString.Length);

I hope this will work for you :)