Convert XML to JSON in Spring Boot

Follow these steps to convert XML to JSON in a Spring Boot application:

  1. Ensure that you have the necessary Jackson dependencies in your project. Add Jackson Dataformat XML library to your project. Jackson Dataformat XML helps to serialize POJOs to XML and deserialize XML to POJOs.
  2. For Maven:

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.15.2</version>
    </dependency>

    For Gradle:

    implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.15.2'

  3. Here's an example code to convert XML to JSON:

  4. First, create a POJO class representing the XML data structure:

    public class MyClass {
    
      private Long id;
      private String name;
      private double amount;
    
      public Long getId() {
        return id;
      }
    
      public void setId(Long id) {
        this.id = id;
      }
    
      public String getName() {
        return name;
      }
    
      public void setName(String name) {
        this.name = name;
      }
    
      public double getAmount() {
        return amount;
      }
    
      public void setAmount(double amount) {
        this.amount = amount;
      }
    
    }

    The code below demonstrates two ways of converting XML data to JSON: one by binding XML data to a POJO Java class and the other by directly converting the parsed XML data to JSON using JsonNode from com.fasterxml.jackson.databind package:

    import java.io.IOException;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.dataformat.xml.XmlMapper;
    
    public class Example {
    
      public static String convertXmlToJson(String xml) {
        XmlMapper xmlMapper = new XmlMapper();
        try {
          MyClass book = xmlMapper.readValue(xml, MyClass.class);
          ObjectMapper objMapper = new ObjectMapper();
          return objMapper.writeValueAsString(book);
        } catch (JsonMappingException ex) {
          ex.printStackTrace();
        } catch (JsonProcessingException ex) {
          ex.printStackTrace();
        }
        return null;
      }
    
      public static String convertXmlToJson2(String xml) {
        XmlMapper xmlMapper = new XmlMapper();
        JsonNode jsonNode;
        try {
          jsonNode = xmlMapper.readTree(xml.getBytes());
          ObjectMapper objMapper = new ObjectMapper();
          return objMapper.writeValueAsString(jsonNode);
        } catch (IOException ex) {
          ex.printStackTrace();
        }
        return null;
      }
    
      public static void main(String[] args) {
    
        String xml = "<MyClass><id>1</id><name>John</name><amount>10.5</amount></MyClass>";
    
        String result1 = convertXmlToJson(xml);
        System.out.println(result1);
    
        String result2 = convertXmlToJson2(xml);
        System.out.println(result2);
    
      }
    
    }

    The output of the above code is as follows:

    {"id":1,"name":"John","amount":10.5}
    {"id":"1","name":"John","amount":"10.5"}