XML to JSON conversion removing the root node











up vote
-2
down vote

favorite












I need the following XML structure converted to json using JSON.org library
I am able to achieve a json structure but need the root node to be truncated and represent the CHallan details node to be always represented as an array even if it has a single element.



Source XML



        <?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_CPINPushRequest xmlns:ns0="http://apcfss.com/CFMS/RCP-FM- I071/CPINPushRequest">
<System>GOAP</System>
<ServiceType>CPINPUSHREQ</ServiceType>
<Signature>ZJLASDFASDFAS</Signature>
<Payload>
<CPIN>10341234567890</CPIN>
<ExpDt>20170720</ExpDt>
<TotalAmt>200</TotalAmt>
<PayerName>abc</PayerName>
<ChallanDtls>
<AcntID>101001001</AcntID>
<Amount>200</Amount>
<AdminZone>10</AdminZone>
</ChallanDtls>
<ChallanDtls>
<AcntID>101001001</AcntID>
<Amount>200</Amount>
<AdminZone>10</AdminZone>
</ChallanDtls>
</Payload>
</ns0:MT_CPINPushRequest>


Required JSON Response



  {             
"System":"GOBH",
"ServiceType":"CPINPUSHREQ",
“Signature”:”ZJLASDFASDFAS” ,
“Payload” :
{
"CPIN":"10341234567890",
"ExpDt":"20170720",
"TotalAmt":"200.00",
“PayerName”:”abc”,
"ChallanDtls":
[
{
"AcntID":"101001001",
"Amount":"200.00",
"AdminZone":"10"
},

]
}
}


CODE



 import java.io.InputStream;
import java.io.OutputStream;


import org.json.JSONObject;
import org.json.XML;


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 XMLToJSONConversion extends AbstractTransformation {


@Override
public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)
throws StreamTransformationException {
InputStream inputStream = transformationInput.getInputPayload().getInputStream();
OutputStream outputStream = transformationOutput.getOutputPayload().getOutputStream();
try {
byte buf = new byte[inputStream.available()];
inputStream.read(buf);
JSONObject xmlJsonObj = XML.toJSONObject(new String(buf, "utf-8"));
String jsonPrettyPrintString = xmlJsonObj.toString(4);
byte bytes = jsonPrettyPrintString.toString().getBytes("UTF-8");
outputStream.write(bytes);
} catch (Exception e) {
getTrace().addDebugMessage("Exception while writing OutputPayload: IOException", e);
throw new StreamTransformationException(e.toString());
}
}
}


Please suggest










share|improve this question
























  • Please format the code for better readability
    – mani deepak
    Nov 10 at 12:36















up vote
-2
down vote

favorite












I need the following XML structure converted to json using JSON.org library
I am able to achieve a json structure but need the root node to be truncated and represent the CHallan details node to be always represented as an array even if it has a single element.



Source XML



        <?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_CPINPushRequest xmlns:ns0="http://apcfss.com/CFMS/RCP-FM- I071/CPINPushRequest">
<System>GOAP</System>
<ServiceType>CPINPUSHREQ</ServiceType>
<Signature>ZJLASDFASDFAS</Signature>
<Payload>
<CPIN>10341234567890</CPIN>
<ExpDt>20170720</ExpDt>
<TotalAmt>200</TotalAmt>
<PayerName>abc</PayerName>
<ChallanDtls>
<AcntID>101001001</AcntID>
<Amount>200</Amount>
<AdminZone>10</AdminZone>
</ChallanDtls>
<ChallanDtls>
<AcntID>101001001</AcntID>
<Amount>200</Amount>
<AdminZone>10</AdminZone>
</ChallanDtls>
</Payload>
</ns0:MT_CPINPushRequest>


Required JSON Response



  {             
"System":"GOBH",
"ServiceType":"CPINPUSHREQ",
“Signature”:”ZJLASDFASDFAS” ,
“Payload” :
{
"CPIN":"10341234567890",
"ExpDt":"20170720",
"TotalAmt":"200.00",
“PayerName”:”abc”,
"ChallanDtls":
[
{
"AcntID":"101001001",
"Amount":"200.00",
"AdminZone":"10"
},

]
}
}


CODE



 import java.io.InputStream;
import java.io.OutputStream;


import org.json.JSONObject;
import org.json.XML;


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 XMLToJSONConversion extends AbstractTransformation {


@Override
public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)
throws StreamTransformationException {
InputStream inputStream = transformationInput.getInputPayload().getInputStream();
OutputStream outputStream = transformationOutput.getOutputPayload().getOutputStream();
try {
byte buf = new byte[inputStream.available()];
inputStream.read(buf);
JSONObject xmlJsonObj = XML.toJSONObject(new String(buf, "utf-8"));
String jsonPrettyPrintString = xmlJsonObj.toString(4);
byte bytes = jsonPrettyPrintString.toString().getBytes("UTF-8");
outputStream.write(bytes);
} catch (Exception e) {
getTrace().addDebugMessage("Exception while writing OutputPayload: IOException", e);
throw new StreamTransformationException(e.toString());
}
}
}


Please suggest










share|improve this question
























  • Please format the code for better readability
    – mani deepak
    Nov 10 at 12:36













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I need the following XML structure converted to json using JSON.org library
I am able to achieve a json structure but need the root node to be truncated and represent the CHallan details node to be always represented as an array even if it has a single element.



Source XML



        <?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_CPINPushRequest xmlns:ns0="http://apcfss.com/CFMS/RCP-FM- I071/CPINPushRequest">
<System>GOAP</System>
<ServiceType>CPINPUSHREQ</ServiceType>
<Signature>ZJLASDFASDFAS</Signature>
<Payload>
<CPIN>10341234567890</CPIN>
<ExpDt>20170720</ExpDt>
<TotalAmt>200</TotalAmt>
<PayerName>abc</PayerName>
<ChallanDtls>
<AcntID>101001001</AcntID>
<Amount>200</Amount>
<AdminZone>10</AdminZone>
</ChallanDtls>
<ChallanDtls>
<AcntID>101001001</AcntID>
<Amount>200</Amount>
<AdminZone>10</AdminZone>
</ChallanDtls>
</Payload>
</ns0:MT_CPINPushRequest>


Required JSON Response



  {             
"System":"GOBH",
"ServiceType":"CPINPUSHREQ",
“Signature”:”ZJLASDFASDFAS” ,
“Payload” :
{
"CPIN":"10341234567890",
"ExpDt":"20170720",
"TotalAmt":"200.00",
“PayerName”:”abc”,
"ChallanDtls":
[
{
"AcntID":"101001001",
"Amount":"200.00",
"AdminZone":"10"
},

]
}
}


CODE



 import java.io.InputStream;
import java.io.OutputStream;


import org.json.JSONObject;
import org.json.XML;


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 XMLToJSONConversion extends AbstractTransformation {


@Override
public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)
throws StreamTransformationException {
InputStream inputStream = transformationInput.getInputPayload().getInputStream();
OutputStream outputStream = transformationOutput.getOutputPayload().getOutputStream();
try {
byte buf = new byte[inputStream.available()];
inputStream.read(buf);
JSONObject xmlJsonObj = XML.toJSONObject(new String(buf, "utf-8"));
String jsonPrettyPrintString = xmlJsonObj.toString(4);
byte bytes = jsonPrettyPrintString.toString().getBytes("UTF-8");
outputStream.write(bytes);
} catch (Exception e) {
getTrace().addDebugMessage("Exception while writing OutputPayload: IOException", e);
throw new StreamTransformationException(e.toString());
}
}
}


Please suggest










share|improve this question















I need the following XML structure converted to json using JSON.org library
I am able to achieve a json structure but need the root node to be truncated and represent the CHallan details node to be always represented as an array even if it has a single element.



Source XML



        <?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_CPINPushRequest xmlns:ns0="http://apcfss.com/CFMS/RCP-FM- I071/CPINPushRequest">
<System>GOAP</System>
<ServiceType>CPINPUSHREQ</ServiceType>
<Signature>ZJLASDFASDFAS</Signature>
<Payload>
<CPIN>10341234567890</CPIN>
<ExpDt>20170720</ExpDt>
<TotalAmt>200</TotalAmt>
<PayerName>abc</PayerName>
<ChallanDtls>
<AcntID>101001001</AcntID>
<Amount>200</Amount>
<AdminZone>10</AdminZone>
</ChallanDtls>
<ChallanDtls>
<AcntID>101001001</AcntID>
<Amount>200</Amount>
<AdminZone>10</AdminZone>
</ChallanDtls>
</Payload>
</ns0:MT_CPINPushRequest>


Required JSON Response



  {             
"System":"GOBH",
"ServiceType":"CPINPUSHREQ",
“Signature”:”ZJLASDFASDFAS” ,
“Payload” :
{
"CPIN":"10341234567890",
"ExpDt":"20170720",
"TotalAmt":"200.00",
“PayerName”:”abc”,
"ChallanDtls":
[
{
"AcntID":"101001001",
"Amount":"200.00",
"AdminZone":"10"
},

]
}
}


CODE



 import java.io.InputStream;
import java.io.OutputStream;


import org.json.JSONObject;
import org.json.XML;


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 XMLToJSONConversion extends AbstractTransformation {


@Override
public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)
throws StreamTransformationException {
InputStream inputStream = transformationInput.getInputPayload().getInputStream();
OutputStream outputStream = transformationOutput.getOutputPayload().getOutputStream();
try {
byte buf = new byte[inputStream.available()];
inputStream.read(buf);
JSONObject xmlJsonObj = XML.toJSONObject(new String(buf, "utf-8"));
String jsonPrettyPrintString = xmlJsonObj.toString(4);
byte bytes = jsonPrettyPrintString.toString().getBytes("UTF-8");
outputStream.write(bytes);
} catch (Exception e) {
getTrace().addDebugMessage("Exception while writing OutputPayload: IOException", e);
throw new StreamTransformationException(e.toString());
}
}
}


Please suggest







java arrays json xml






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 7:49

























asked Nov 10 at 7:53









Sushant Shinde

11




11












  • Please format the code for better readability
    – mani deepak
    Nov 10 at 12:36


















  • Please format the code for better readability
    – mani deepak
    Nov 10 at 12:36
















Please format the code for better readability
– mani deepak
Nov 10 at 12:36




Please format the code for better readability
– mani deepak
Nov 10 at 12:36

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237057%2fxml-to-json-conversion-removing-the-root-node%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237057%2fxml-to-json-conversion-removing-the-root-node%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly