Jackson – Converting between XML and JSON

Lokesh Gupta

Learn to convert between JSON and XML strings using Jackson’s JsonMapper and XmlMapper classes with simple and easy-to-understand examples.

1. Setup

Add the latest version of Jackson and jackson-dataformat-xml module to support XML parsing, if we have not already added to the project.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>${jackson.version}</version>
</dependency>

For demo purposes, we will use the following Data class.

@lombok.Data
class Data {
  private Long id;
  private String name;
}

2. Converting from XML to JSON

We can read the XML from various sources, even from an API call. After we have the XML string, we can use XmlMapper to parse into POJO.

Once we have the POJO, we can write it to JSON format as any regular class.

@Test
public void testXmlToJson() throws IOException {
  String xml = "<Data><id>1</id><name>Lokesh</name></Data>";

  XmlMapper xmlMapper = new XmlMapper();
  Data dataInstance = xmlMapper.readValue(xml.getBytes(), Data.class);

  JsonMapper jsonMapper = new JsonMapper();
  String json = jsonMapper.writeValueAsString(dataInstance);

  Assertions.assertEquals("{\"id\":1,\"name\":\"Lokesh\"}", json);
}

If we do not have the POJO class to parse the XML, we can use the Jackson provided JsonNode or ObjectNode classes.

@Test
public void testXmlToJsonUsingObjectNode() throws IOException {
  String xml = "<Data><id>1</id><name>Lokesh</name></Data>";

  XmlMapper xmlMapper = new XmlMapper();
  ObjectNode dataInstance = xmlMapper.readValue(xml.getBytes(), ObjectNode.class);

  JsonMapper jsonMapper = new JsonMapper();
  String json = jsonMapper.writeValueAsString(dataInstance);

  Assertions.assertEquals("{\"id\":\"1\",\"name\":\"Lokesh\"}", json);
}

3. Converting from JSON to XML

The conversion from a JSON string to XML is very much similar to the previous example. First, we parse the JSON string to POJO, and then POJO is written to the XML.

@Test
public void testJsonToXml() throws JsonProcessingException {

  String json = "{\"id\": 1,\"name\": \"Lokesh\"}";

  JsonMapper jsonMapper = new JsonMapper();
  Data dataInstance = jsonMapper.readValue(json, Data.class);

  XmlMapper xmlMapper = new XmlMapper();
  String xml = xmlMapper.writeValueAsString(dataInstance);

  Assertions.assertEquals("<Data><id>1</id><name>Lokesh</name></Data>", xml);
}

Again, we do not have an intermediate POJO class to parse the XML, we can directly use the JsonNode or ObjectNode classes.

Both classes, JsonNode or ObjectNode, will produce an XML representation with the root element as <ObjectNode>.

@Test
public void testJsonToXmlUsingObjectNode() throws JsonProcessingException {

  String json = "{\"id\": 1,\"name\": \"Lokesh\"}";

  JsonMapper jsonMapper = new JsonMapper();
  ObjectNode objectNode = jsonMapper.readValue(json, ObjectNode.class);

  XmlMapper xmlMapper = new XmlMapper();
  String xml = xmlMapper.writeValueAsString(objectNode);

  Assertions.assertEquals("<ObjectNode><id>1</id><name>Lokesh</name></ObjectNode>", xml);
}

4. Conclusion

In this short Jackson tutorial, we learned to convert from JSON string to XML string, and convert XML to JSON. We learned to use the custom POJO as well as Jackson provided ObjectNode and JsonNode classes.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.