Jackson XML to JSON Converter removes multiple child records

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!

+4
source share
2 answers

I was able to solve this problem using the org.json API to convert the source XML to JSONObject and then to JSON from the Jackson API.

the 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);
}

...
...

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>

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

, XML "" ( JSON, .. /) "" ( ). (POJO) Java, XML-, - Java List , "Array"; "Object" - mapper XML java.util.Map, , XML- "" . "" , ().

XmlMapper: .

0

All Articles