Learn XML and JSON basics with examples
In this blog post, We will discuss XML and JSON basics, pros/cons, and also convert JSON to XML or XML to JSON in java.
What is JSON?
JSON
is abbreviated as javascript Object Notation
.
JSON
is the independent format for exchanging data JSON is a simple format that contains key-pair values. JSON and XML are data formats used to transfer data between different systems of enterprise applications. JSON is lightweight and simple to read.
JSON Sample data file
Following is a sample JSON file that contains simple key and value pairs
`{ "id" : 1.0,
"name" : "Kiran",
"sal" : 5000,
}`
XML Basics
XML is an extensive markup language that starts and end tag and follows certain data validations. XML file can be validates with XML Schema. XML files have data attributes.
Reading XML is very easy as most browsers and editors support the simple clickable readable format.
XML sample data file
Most of the companies which offer REST APIS expose the data in either XML or JSON
Facebook and Twitter expose their REST API using JSON only.
Clients consume those REST APIs and use programming techniques to process them.
<main>
<subtag>Sub tag<subtag>
</subtag></subtag>
</main>
Difference between JSON and XML, pros and cons
Each one has advantages and disadvantages and is listed below.
JSON | XML |
---|---|
Abbreviated as Javascript Simple notation language | Abbreviated as Extensible Markup Language |
Markup language | Meta Language |
JSON contains data of typed values | xml contains data of typeless values and structure of rules as per XML schema |
Easy to understand and lightweight | Complex to read and understand |
Easy to parse JSON formats | Parsers are needed to parse XML |
End tags are available | Each tag has an end tag |
Human readable | Less human readable |
Modenr Applications are using this as standard format data to interchange between different machines, applications, or layers | Legacy Applications still use XML type data to exchange data between applications or machines. |
XML and JSON common capabilities
- Both describe hierarchical data.
- Both are easy to learn and understand Language is independent.
- Both are used to share data between different systems
- Both have support in all languages
Below, We are going to discuss how to convert XML to JSON to/from in Java. We have many ways to convert to do it in java
How to convert JSON to XML in java
com. gson.gson library provides API for reading JSON objects and to convert into XML objects.
import org.json.JSONObject;
import org.json.XML;
public class JsonToXmlConvert {
public static void main(String[] args) throws Exception {
String jsonObject = "{'json key':'json value'}";
JSONObject json = new JSONObject(jsonObject);
String xml = XML.toString(json);
}
}
How to Convert XML to JSON in java?
It is very simple to convert from XML and JSON in the java.org.json.json library provides API to do this. First get dependency org.json.json library if it maven project else, You need this jar file in your classpath.
String xmlInString = ");";
JSONObject jsonObject = XML.toJSONObject(xmlInString);
System.out.println(jsonObject )
{
"emp": {
"name": "kiran"
}
}
Please share this post if you liked it.