Convert JSON to XML in Java



In this article, we will learn how to convert a JSON object into XML format in Java. But first, let's understand what JSON and XML are.

  • JSON is a lightweight data-interchange format, and the format of JSON is like a key-value pair.
  • XML (Extensible Markup Language) is also a way to store data, but unlike JSON, it uses tags to make the proper structure.

Why Do we Convert JSON to XML?

There can be multiple situations where we would need to convert JSON to XML. Here are some of the reasons:

  • XML is mostly used in web services and APIs, so we need to exchange data that's why we need to convert JSON to XML.
  • Some systems only work with XML. In these types of cases, to make the system understand the data, we need to convert JSON to XML.
  • XML has rules that ensure that the data is correct and has no mistakes, which avoids errors.

For example, you have a JSON object with details about a person, like their name, age, and address. You need to convert this JSON object into XML so the other system can understand it.

Here is an example of a JSON object:

{
   "name": "Ansh",
   "age": 22,
   "address": {
      "street": "Jublee Hills",
      "city": "Hyderabad",
      "state": "Telangana"
   }
}

And here is the equivalent XML format:

<person>
<name>Ansh</name>
<age>22</age>
<address>
   <street>Jublee Hills</street>
   <city>Hyderabad</city>
   <state>Telangana</state>
</address>
</person>

In this example, the JSON object has details about a person, like their name, age, and address. The XML format shows the same information using its own tag structure.

Adding the json.simple Library

First, we need to download the library from its official website, or you can just add it to your project if you are using Maven or Gradle. If you're using Maven, add this to your pom.xml file:

<dependency>
   <groupId>org.json</groupId>
   <artifactId>json</artifactId>
   <version>20210307</version>
</dependency>

Creating a JSON Object

We can create a JSON object by instantiating the org.json.JSONObject class. We can add elements to this object using the put() method.

Here's an example code snippet to create a JSON object:

import org.json.JSONObject;

public class JsonToXmlExample {
   public static void main(String[] args) {
      // Create a JSON object
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("name", "Ansh");
      jsonObject.put("age", 22 );
      JSONObject address = new JSONObject();
      address.put("street", "Jublee Hills");
      address.put("city", "Hyderabad");
      address.put("state", "Telangana");
      jsonObject.put("address", address);

      System.out.println("JSON Object: " + jsonObject.toString(2));
   }
}

Following is the output of the above code:

JSON Object: {
   "name": "Ansh",
   "age": 22,
   "address": {
      "street": "Jublee Hills",
      "city": "Hyderabad",
      "state": "Telangana"
   }
}

Now, let's see how to convert a JSON object to XML format in Java using the org.json library.

How to Convert JSON to String XML in Java

The org.json.XML represents an XML document in Java. It provides various methods to convert XML text to JSONObject and vice-versa. The toString() method of this class accepts an object representing JSON, converts it into an XML string. Following is the syntax of this method -

toString(Object object)

Where, object represents the JSON object.

Example

Following is a program to convert the JSON object to XML String -

import org.json.XML;

public class JsonToXmlExample {
   public static void main(String[] args) {
      // Create a JSON object
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("name", "Ansh");
      jsonObject.put("age", 22);
      JSONObject address = new JSONObject();
      address.put("street", "Jublee Hills");
      address.put("city", "Hyderabad");
        address.put("state", "Telangana");
      jsonObject.put("address", address);

      // Convert JSON to XML
      String xml = XML.toString(jsonObject);
      System.out.println(xml);
   }
}

Following is the output of the above code:

<name>Ansh</name>
<age>22</age>
<address>
<street>Jublee Hills</street>
<city>Hyderabad</city>
<state>Telangana</state>
</address>
Updated on: 2025-04-21T18:54:40+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements