Learn XML and JSON basics with examples

In this blog post, we will discuss the basics of XML and JSON, their respective pros and cons, and how to convert JSON to XML or XML to JSON in Java.

What is JSON?

JSON stands for JavaScript Object Notation. It is an independent data interchange format consisting of key-value pairs.

JSON is lightweight and easy to read, making it ideal for data exchange between different systems in enterprise applications.

JSON Sample Data File

Here’s a sample JSON file containing simple key-value pairs

{   "id" : 1.0,
    "name" : "abc",
    "sal" : 5000,
}

XML Basics

XML, or Extensible Markup Language, is a versatile markup language characterized by start and end tags, along with certain data validation rules.

XML files can be validated with XML Schema and often include data attributes.

Reading XML is straightforward as most browsers and editors support its clickable, readable format.

XML Sample Data File

Many companies offering REST APIs expose data in either XML or JSON formats. For instance, Facebook and Twitter primarily use JSON for their REST APIs.

Clients consume these APIs and utilize various programming techniques to process the data.

<main>
  <subtag>Sub tag<subtag>
  </subtag></subtag>
</main>

Difference Between JSON and XML: Pros and Cons

Both JSON and XML have distinct advantages and disadvantages, as listed below:

JSONXML
Abbreviated as JavaScript Object NotationAbbreviated as Extensible Markup Language
Markup languageMeta Language
Contains typed data valuesContains typeless data values and follows XML schema rules
Lightweight and easy to understandComplex to read and understand
Easy to parse JSON formatsRequires parsers for XML parsing
Uses end tagsRequires end tags for each element
More human-readableLess human readable
Commonly used in modern applications for data interchange between applications or machinesLegacy applications often use XML for data exchange

XML and JSON common capabilities

  1. Both describe hierarchical data.
  2. Both are easy to learn and language-independent.
  3. Both are used to share data between different systems.
  4. Both have support in all programming languages.

Now, let’s discuss how to convert XML to JSON and vice versa in Java. There are several ways to achieve this in Java.

How to Convert JSON to XML in Java

The org.json library provides an API for reading JSON objects and converting them into XML objects.

import org.json.JSONObject;
import org.json.XML;

public class JsonToXmlConvert {
    public static void main(String[] args) {
        String jsonObject = "{'json key':'json value'}";
        JSONObject json = new JSONObject(jsonObject);
        String xml = XML.toString(json);
        System.out.println(xml);
    }
}

How to Convert XML to JSON in Java

Converting from XML to JSON is also straightforward in Java. The org.json library offers an API for this purpose.

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"
  }
}