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
java arrays json xml
add a comment |
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
java arrays json xml
Please format the code for better readability
– mani deepak
Nov 10 at 12:36
add a comment |
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
java arrays json xml
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
java arrays json xml
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Please format the code for better readability
– mani deepak
Nov 10 at 12:36