I am using following code to convert my source XML to JSON. However, this code removes any multiple occurrence of child records in source XML and output JSON only contains last child record.
How do I get Jackson XML to JSON converter to output all child records in JSON?
Code
XmlMapper xmlMapper = new XmlMapper();
Map entries = xmlMapper.readValue(new File("source.xml"), LinkedHashMap.class);
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writer().writeValueAsString(entries);
System.out.println(json);
Source XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<File>
<NumLeases>1</NumLeases>
<FLAG>SUCCESS</FLAG>
<MESSAGE>Test Upload</MESSAGE>
<Lease>
<LeaseVersion>1</LeaseVersion>
<F1501B>
<NEDOCO>18738</NEDOCO>
<NWUNIT>0004</NWUNIT>
<NTRUSTRECORDKEY>12</NTRUSTRECORDKEY>
</F1501B>
<F1501B>
<NEDOCO>18739</NEDOCO>
<NWUNIT>0005</NWUNIT>
<NTRUSTRECORDKEY>8</NTRUSTRECORDKEY>
</F1501B>
</Lease>
</File>
Actual Output
{
"NumLeases": "1",
"FLAG": "SUCCESS",
"MESSAGE": "Test Upload",
"Lease": {
"LeaseVersion": "1",
"F1501B": {
"NEDOCO": "18739",
"NWUNIT": "0005",
"NTRUSTRECORDKEY": "8"
}
}
}
Expected Output
{
"NumLeases": "1",
"FLAG": "SUCCESS",
"MESSAGE": "Test Upload",
"Lease": {
"LeaseVersion": "1",
"F1501B": [
{
"NEDOCO": "18738",
"NWUNIT": "0004",
"NTRUSTRECORDKEY": "12"
},
{
"NEDOCO": "18739",
"NWUNIT": "0005",
"NTRUSTRECORDKEY": "8"
}
]
}
}
Any help shall be appreciated. Thanks!
There are some support compatibility features -- such as ability to optionally use JAXB annotations -- but fundamentally Jackson is a pure JSON/Java data mapper and tries to minimize impedance between JSON and Java data models. JAXB was part of standard JDK until JDK 1.8.
Reading XML We can also read XML, using the various readValue APIs that are part of provided by the ObjectMapper. For example, reading some XML from an InputStream into a Java Bean: MyBean bean = objectMapper.
I was able to get the solution to this problem by using org.json API to convert source XML to JSONObject and then to JSON by Jackson API.
Code
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.json.XML;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
...
...
try (InputStream inputStream = new FileInputStream(new File(
"source.xml"))) {
String xml = IOUtils.toString(inputStream);
JSONObject jObject = XML.toJSONObject(xml);
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Object json = mapper.readValue(jObject.toString(), Object.class);
String output = mapper.writeValueAsString(json);
System.out.println(output);
}
...
...
Source XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<File>
<NumLeases>1</NumLeases>
<FLAG>SUCCESS</FLAG>
<MESSAGE>Test Upload</MESSAGE>
<Lease>
<LeaseVersion>1</LeaseVersion>
<F1501B>
<NEDOCO>18738</NEDOCO>
<NWUNIT>0004</NWUNIT>
<NTRUSTRECORDKEY>12</NTRUSTRECORDKEY>
</F1501B>
<F1501B>
<NEDOCO>18739</NEDOCO>
<NWUNIT>0005</NWUNIT>
<NTRUSTRECORDKEY>8</NTRUSTRECORDKEY>
</F1501B>
</Lease>
</File>
Output
{
"File" : {
"NumLeases" : "1",
"FLAG" : "SUCCESS",
"MESSAGE" : "Test Upload",
"Lease" : {
"LeaseVersion" : "1",
"F1501B" : [ {
"NEDOCO" : "18738",
"NWUNIT" : "0004",
"NTRUSTRECORDKEY" : "12"
}, {
"NEDOCO" : "18739",
"NWUNIT" : "0005",
"NTRUSTRECORDKEY" : "8"
} ]
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With