Converting JSON Object to XML and XML to JSON Object
Jun 11, 2020
Make sure to subscribe to our newsletter and be the first to know the news.
Jun 11, 2020
Make sure to subscribe to our newsletter and be the first to know the news.
This blog post we are going to understand a important method of converting a JSONObject into XML String and and XML String data into an JSONObject.
Before JSON came into picture , XML (eXtensive Markup Language) was the way in which applications used to communicate with each other, as JSON came into the market there are certain scenarios where JSON Framework still needs to convert an XML output into JSON and vice versa for bridging the gaps.
JSON-Java Framework have a class by name XML which have static methods to convert the JSON String to XML and back.
public static String toString(JSONObject object)
Converts the passed JSONObject to a XML String, it comes with an additional parameter of String tag (I have used the extra parameter version in the code below), which will keep all the XML data under the specified tag.
public static JSONObject toJSONObject(String s)
Converts the XML String back to JSON Object.
Let us consider the following code and lets convert the JSON Object into XML
import org.json.*;
public class JSONToXML
{
public static void main(String[] args)
{
String jsonString="{orderId:1,custName:Shaun,orderDate:2020-01-02,deliveryDate:2020-01-05,cart:[{productId:1,qty:2,price:23},{productId:4,qty:4,price:32}],address:{street:Street 1,lane:Lane 5,building: Building XYZ,landmark:Next to Bus Stop}}";
JSONObject obj=new JSONObject(jsonString);
System.out.println("JSON String:\n\n"+obj.toString(5)+"\n\n-----------------------------------------");
String xmlString=XML.toString(obj,"order-data");
System.out.println("XML :\n\n"+xmlString);
}
}
Output:
JSON String:
{
"address": {
"street": "Street 1",
"landmark": "Next to Bus Stop",
"lane": "Lane 5",
"building": "Building XYZ"
},
"orderId": 1,
"custName": "Shaun",
"deliveryDate": "2020-01-05",
"orderDate": "2020-01-02",
"cart": [
{
"productId": 1,
"price": 23,
"qty": 2
},
{
"productId": 4,
"price": 32,
"qty": 4
}
]
}
-----------------------------------------
XML :
<order-data><address><street>Street 1</street><landmark>Next to Bus Stop</landmark><lane>Lane 5</lane><building>Building XYZ</building></address><orderId>1</orderId><custName>Shaun</custName><deliveryDate>2020-01-05</deliveryDate><orderDate>2020-01-02</orderDate><cart><productId>1</productId><price>23</price><qty>2</qty></cart><cart><productId>4</productId><price>32</price><qty>4</qty></cart></order-data>
</code></pre>
Now let's try XML to JSON
import org.json.*;
public class XMLToJSON
{
public static void main(String[] args)
{
String xmlString="<website><address><area>Mumbai</area><plot>53</plot><type>office</type><sector>20</sector></address><address><area>Navi Mumbai</area><plot>35</plot><type>hq</type><sector>10</sector></address><serverDetails><hostingStatus>active</hostingStatus><serverOS>Linux</serverOS></serverDetails><phone>12345</phone><phone>54321</phone><phone>098765</phone><phone>999888</phone><author>Zartab Nakhwa</author><name>Code with Z</name><url>www.codewithz.com</url></website>";
System.out.println("Input XML String:"+xmlString+"\n\n");
JSONObject obj=XML.toJSONObject(xmlString);
System.out.println("Output JSON String:\n\n"+obj.toString(5)+"\n\n-----------------------------------------");
}
}
Input XML String:<website><address><area>Mumbai</area><plot>53</plot><type>office</type><sector>20</sector></address><address><area>Navi Mumbai</area><plot>35</plot><type>hq</type><sector>10</sector></address><serverDetails><hostingStatus>active</hostingStatus><serverOS>Linux</serverOS></serverDetails><phone>12345</phone><phone>54321</phone><phone>098765</phone><phone>999888</phone><author>Zartab Nakhwa</author><name>Code with Z</name><url>www.codewithz.com</url></website>
Output JSON String:
{"website": {
"address": [
{
"area": "Mumbai",
"plot": 53,
"type": "office",
"sector": 20
},
{
"area": "Navi Mumbai",
"plot": 35,
"type": "hq",
"sector": 10
}
],
"serverDetails": {
"hostingStatus": "active",
"serverOS": "Linux"
},
"phone": [
12345,
54321,
"098765",
999888
],
"author": "Zartab Nakhwa",
"name": "Code with Z",
"url": "www.codewithz.com"
}}