
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert XML to JSON Array in Java
Xml is a markup language that is used to store or transport data. It uses tags to define the data. There are libraries also available for converting XML to JSON. Those are:
- org.json
- Jackson
- Gson
Let's learn how to convert XML to JSON using various libraries.
Converting XML to JSON using org.json
For converting XML to JSON, we will use the org.json library. It is a simple library that is used for converting XML to JSON and vice versa. We can use the XML.toJSONObject() method to convert XML to JSON.
We cannot use the org.json library without downloading or adding it to the project. As it is not a part of the Java standard library. To use org.json library without downloading it, we can use it from the Maven repository.
Download a jar file from here. Or, if you are using Maven, add this to your pom.xml
file -
<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20210307</version> </dependency>
Steps to convert XML to JSON using the org.json library:
- First, import the org.json library as discussed above.
- Then create a XML string.
- Then we will create an instance of XML class.
- Next, we will convert the XML to JSON using the toJSONObject() method of XML.
- Finally, we will print the JSON string.
Example
Following is the code to convert XML to JSON using org.json library:
import org.json.JSONObject; import org.json.XML; public class XmlToJson { public static void main(String[] args) { // Create a XML string String xmlString = "<?xml version="1.0" encoding="UTF-8"?>\n" + "<note>\n" + " <to>Tove</to>\n" + " <from>Jani</from>\n" + " <heading>Reminder</heading>\n" + " <body>Don't forget me this weekend!</body>\n" + "</note>"; // Convert XML to JSON JSONObject jsonObject = XML.toJSONObject(xmlString); // Print the JSON string System.out.println(jsonObject.toString(4)); } }
Following is the output of the above code:
{ "note": { "to": "Tove", "from": "Jani", "heading": "Reminder", "body": "Don't forget me this weekend!" } }
Converting XML to JSON using Jackson
Jackson is a third-party Java library developed by FasterXML. It is used for converting Java objects to JSON and vice versa. To know more about Jackson, refer to the Jackson tutorial.
We can use the XmlMapper class of the Jackson library to convert XML to JSON. The XmlMapper class is a subclass of ObjectMapper class.
Similar to the org.json library, we can either download the jar file or we can use it from the Maven repository. Download a jar file from here. Or, if you are using Maven, add this to your pom.xml
file:
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.12.3</version> </dependency>
Steps to convert XML to JSON using the Jackson library:
- First, import the Jackson library as discussed above.
- Then create a XML string.
- Then we will create an instance of XmlMapper class.
- Next, we will convert the XML to JSON using the readTree() method of XmlMapper.
- Finally, we will print the JSON string.
Example
Following is the code to convert XML to JSON using Jackson library:
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; public class XmlToJson { public static void main(String[] args) { // Create a XML string String xmlString = "<?xml version="1.0" encoding="UTF-8"?>\n" + "<note>\n" + " <to>Tove</to>\n" + " <from>Jani</from>\n" + " <heading>Reminder</heading>\n" + " <body>Don't forget me this weekend!</body>\n" + "</note>"; // Create an XmlMapper instance XmlMapper xmlMapper = new XmlMapper(); try { // Convert XML to JSON JsonNode jsonNode = xmlMapper.readTree(xmlString.getBytes()); // Print the JSON string ObjectMapper objectMapper = new ObjectMapper(); String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } }
Following is the output of the above code:
{ "note" : { "to" : "Tove", "from" : "Jani", "heading" : "Reminder", "body" : "Don't forget me this weekend!" } }
Converting XML to JSON using Gson
Gson is a third-party Java library developed by Google. It is used for converting Java objects to JSON and vice versa. To know more about Gson, refer to the Gson tutorial.
We can use the Gson class of the Gson library to convert XML to JSON. The Gson class is a subclass of JsonElement class. Similar to org.json library, we can either download the jar file or we can use it from the Maven repository. Download a jar file from here. Or, if you are using Maven, add this to your pom.xml
file:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.9</version> </dependency>
Steps to convert XML to JSON using Gson library:
- First, import the Gson library as discussed above.
- Then create a XML string.
- Then we will create an instance of Gson class.
- Next, we will convert the XML to JSON using the fromJson() method of Gson.
- Finally, we will print the JSON string.
Example
Following is the code to convert XML to JSON using Gson library:
import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; import com.google.gson.JsonElement; public class XmlToJson { public static void main(String[] args) { // Create a XML string String xmlString = "<?xml version="1.0" encoding="UTF-8"?>\n" + "<note>\n" + " <to>Tove</to>\n" + " <from>Jani</from>\n" + " <heading>Reminder</heading>\n" + " <body>Don't forget me this weekend!</body>\n" + "</note>"; // Create a Gson instance Gson gson = new Gson(); // Convert XML to JSON JsonElement jsonElement = JsonParser.parseString(xmlString); JsonObject jsonObject = gson.fromJson(jsonElement, JsonObject.class); // Print the JSON string System.out.println(gson.toJson(jsonObject)); } }
Following is the output of the above code:
{ "note": { "to": "Tove", "from": "Jani", "heading": "Reminder", "body": "Don't forget me this weekend!" } }