I am using the following code to convert my source XML to JSON. However, this code removes any multiple occurrences of child records in the source XML, and the JSON output contains only the last child record.
How to get Jackson XML to JSON converter to output all child records in JSON?
the 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);
XML source
<?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 Result
{
"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 should be appreciated. Thank!
source
share