Jackson XML to JSON converter removes multiple child records

Aman picture Aman · Apr 4, 2016 · Viewed 9k times · Source

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!

Answer

Aman picture Aman · Apr 7, 2016

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"
      } ]
    }
  }
}