In this article, we will discuss how to convert JSON file to XML file using JSONObject
1. Required libraries :
1.1 Maven Co-ordinates :
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
</dependency>
1.2 JAR files to download :
Download below jar files and include them in Project classpath –
1.3 Project class-path :
2. JSON to XML conversion :
In the below illustration, we will convert JSON to XML file using JSONObject,
- 1st step is to read the contents of the JSON file to a String object
- 2nd step is to convert XML in String format to JSONObject
- by instantiating a new object of type JSONObject passing xmlInString as a constructor–argument
- 3rd step is to convert JSONObject into XML using XML.toString(jsonObject) method by passing json object obtained from 2nd step
- 4th step is to store converted XML into a file using Files.write() method passing 2 input-arguments
- File location
- XML String contents in bytes format
JSON file contents – Person.json:
{
"title" : "Mr.",
"firstName" : "Sam",
"lastName" : "Anton",
"address" : {
"flatNumber" : "BV-1025",
"buildingName" : "Shivaji",
"plotNumber" : "1093",
"sector" : "Sector 19",
"city" : "Bengaluru",
"state" : "Karnataka",
"country" : "India",
"nodeName" : "South Bengaluru"
},
"items" : [ {
"itemName" : "Television",
"itemBoughtYear" : "2014"
}, {
"itemName" : "Washing Machine",
"itemBoughtYear" : "2020"
}, {
"itemName" : "Refrigerator",
"itemBoughtYear" : "2011"
}, {
"itemName" : "Grinder",
"itemBoughtYear" : "2012"
}, {
"itemName" : "Computer",
"itemBoughtYear" : "2010"
} ]
}
JsonToXmlConversion.java
package in.bench.resources.yaml.conversion;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.json.JSONObject;
import org.json.XML;
public class JsonToXmlConversion {
// main method
public static void main(String[] args) {
try {
// 1st step - read JSON contents
String jsonContent = new String(Files.readAllBytes(Paths.get(
"C:\\Users\\Demo\\eclipse-workspace\\YamlConversion\\Person.json")));
// 2nd step - convert JSON to XML
String xmlResult = convertJsonToXml(jsonContent);
// print to console
System.out.println("JSON to XML conversion :- \n\n" + xmlResult);
// store converted JSON file
storeConvertedXmlInFile(xmlResult);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* This method converts JSON String to unformatted XML
*
* @param json
* @return
*/
private static String convertJsonToXml(String json) {
// create JSONObject
JSONObject jsonObject = new JSONObject(json);
// return xml
return XML.toString(jsonObject);
}
/**
* This method stores converted XML in Project class-path
*
* @param xmlString
*/
private static void storeConvertedXmlInFile(String xmlString) {
// define path to write Json file
Path filename = Paths.get(
"C:\\Users\\Demo\\eclipse-workspace\\YamlConversion\\JSON_to_XML_Person.xml");
try {
// write to the above file path
Files.write(filename, xmlString.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output in console :
JSON to XML conversion :-
<firstName>Sam</firstName>
<lastName>Anton</lastName>
<address>
<nodeName>South Bengaluru</nodeName>
<buildingName>Shivaji</buildingName>
<country>India</country>
<city>Bengaluru</city>
<flatNumber>BV-1025</flatNumber>
<plotNumber>1093</plotNumber>
<state>Karnataka</state>
<sector>Sector 19</sector>
</address>
<title>Mr.</title>
<items>
<itemName>Television</itemName>
<itemBoughtYear>2014</itemBoughtYear>
</items>
<items>
<itemName>Washing Machine</itemName>
<itemBoughtYear>2020</itemBoughtYear>
</items>
<items>
<itemName>Refrigerator</itemName>
<itemBoughtYear>2011</itemBoughtYear>
</items>
<items>
<itemName>Grinder</itemName>
<itemBoughtYear>2012</itemBoughtYear>
</items>
<items>
<itemName>Computer</itemName>
<itemBoughtYear>2010</itemBoughtYear>
</items>
Converted XML file -> JSON_to_XML_Person.xml
<firstName>Sam</firstName>
<lastName>Anton</lastName>
<address>
<nodeName>South Bengaluru</nodeName>
<buildingName>Shivaji</buildingName>
<country>India</country>
<city>Bengaluru</city>
<flatNumber>BV-1025</flatNumber>
<plotNumber>1093</plotNumber>
<state>Karnataka</state>
<sector>Sector 19</sector>
</address>
<title>Mr.</title>
<items>
<itemName>Television</itemName>
<itemBoughtYear>2014</itemBoughtYear>
</items>
<items>
<itemName>Washing Machine</itemName>
<itemBoughtYear>2020</itemBoughtYear>
</items>
<items>
<itemName>Refrigerator</itemName>
<itemBoughtYear>2011</itemBoughtYear>
</items>
<items>
<itemName>Grinder</itemName>
<itemBoughtYear>2012</itemBoughtYear>
</items>
<items>
<itemName>Computer</itemName>
<itemBoughtYear>2010</itemBoughtYear>
</items>
In the following article, we will discuss same JSON to XML conversion using Jackson or ObjectMapper in detail
Related Articles :
- Gson – Java Object to JSON conversion
- Jackson – Java Object to JSON conversion
- Java – How to convert an Object to YAML using ObjectMapper ?
- Java – How to convert YAML to an Object using ObjectMapper ?
- Java – How to convert YAML to JSON using ObjectMapper ?
- Java – How to convert JSON to YAML using ObjectMapper ?
- Java – How to convert XML to JSON using ObjectMapper ?
- Java – How to convert JSON to XML using ObjectMapper ?
- Java – How to convert XML to YAML using ObjectMapper ?
- Java – How to convert YAML to XML using ObjectMapper ?
- Java – How to pretty-print JSON using ObjectMapper ?
- Java – How to convert XML to JSON using JSONObject ?
- Java – How to convert JSON to XML using JSONObject ?
- Java – How to pretty-print JSON using JSONObject ?
References :
Happy Coding !!
Happy Learning !!