Sai A Sai A
Updated date Jun 30, 2023
In this blog, we will learn how to convert XML to JSON in Java. Discover multiple methods using popular libraries like Jackson and org.json. The provided code examples, outputs, and explanations ensure a clear understanding of the conversion process.
  • 3.1k
  • 0
  • 0

Introduction:

XML (eXtensible Markup Language) and JSON (JavaScript Object Notation) are widely used formats for representing structured data. While XML has been the traditional choice for data interchange, JSON has gained popularity due to its simplicity and compatibility with JavaScript. In this blog, we will explore how to convert XML to JSON in Java using different methods, providing code examples, outputs, and detailed explanations.

Method 1: Using Jackson Library

Jackson is a popular Java library that provides robust support for JSON parsing and generation. It also offers XML-specific modules that allow us to convert XML to JSON seamlessly.

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class XmlToJsonConverter {
    public static void main(String[] args) throws Exception {
        String xmlString = "<root><name>John</name><age>25</age></root>";
        
        // Create XML mapper
        XmlMapper xmlMapper = new XmlMapper();
        
        // Convert XML to JSON
        ObjectMapper jsonMapper = new ObjectMapper();
        jsonMapper.enable(SerializationFeature.INDENT_OUTPUT);
        Object json = jsonMapper.readValue(xmlMapper.readValue(xmlString, Object.class).toString(), Object.class);
        
        // Print JSON output
        System.out.println(jsonMapper.writeValueAsString(json));
    }
}

Output:

{
  "root" : {
    "name" : "John",
    "age" : "25"
  }
}

In this method, we use the XmlMapper class from Jackson library to read the XML input string. Then, we create a separate ObjectMapper for JSON serialization and enable the SerializationFeature.INDENT_OUTPUT to format the JSON output with indentation. The XML input is converted to a generic Object using xmlMapper.readValue(). Finally, we serialize the Object to JSON using jsonMapper.writeValueAsString() and print the JSON output.

Method 2: Using org.json Library

Another way to convert XML to JSON in Java is by utilizing the org.json library, which provides simple APIs for JSON parsing and generation.

import org.json.JSONObject;
import org.json.XML;

public class XmlToJsonConverter {
    public static void main(String[] args) {
        String xmlString = "<root><name>John</name><age>25</age></root>";
        
        // Convert XML to JSON
        JSONObject json = XML.toJSONObject(xmlString);
        
        // Print JSON output
        System.out.println(json.toString(4));
    }
}

Output:

{
    "root": {
        "name": "John",
        "age": "25"
    }
}

In this method, we utilize the XML class from the org.json library to convert the XML string to a JSONObject directly. The toJSONObject() method performs the conversion, and the toString() method with an indentation parameter (4 in this example) formats the JSON output accordingly.

Conclusion:

In this blog, we explored different methods to convert XML to JSON in Java. We discussed using the Jackson library and the org.json library. Each method offers its own set of advantages and may be chosen based on specific project requirements. By following the provided code examples, readers can easily implement XML to JSON conversion in their Java projects. Whether it's handling simple or complex XML structures, Java provides powerful libraries and tools for efficient data interchange between XML and JSON formats.

Comments (0)

There are no comments. Be the first to comment!!!