In this quick article, we’ll explore how to convert XML to JSON in Java.

Java community didn’t provide any capability to convert XML to JSON in standard JDK. However, several third-party libraries are available for conversion between JSON and XML.

 
We can use the org.json.XML class from JSON in Java package to convert an XML to JSON in Java. Following is a simple example demonstrating usage of this library:

Download Code

 
Output:

{
  "root": {
    "firstName": "John",
    "lastName": "Snow",
    "address": {
        "city": "Brooklyn, NY",
        "street": "237 Harrison Street",
        "postalCode": 11238,
        "state": "New York"
    },
    "age": 25,
    "spouse": "",
    "phoneNumbers": [
        {
            "number": "212 555-3346",
            "type": "mobile"
        },
        {
            "number": "646 555-4567",
            "type": "fax"
        }
    ]
  }
}

 
Before we wind up this post, here’s the maven dependency for JSON In Java.

That’s all about converting XML to JSON in Java.

 
Also See: Convert XML to JSON online