Home » JSON vs XML

JSON vs XML

Before knowing about the differences between JSON and XML, we should be aware of the definition of json and xml.

What is json?

JSON stands for JavaScript object notation. JSON has been derived from javascript, where javascript is a programming language. It was originally created to hold the structured data that could be used in javascript. JSON became so popular that it is used for data for all kinds of applications. It is the most popular way of sending the data for Web APIs.

Basic data types supported by json are:

  • Strings: Characters that are enclosed in single or double quotation marks.
  • Number: A number could be integer or decimal, positive or negative.
  • Booleans: The Boolean value could be either true or false without any quotation marks.
  • Null: Here, null means nothing without any quotation marks.

In addition to basic data types, json has arrays and objects.

Arrays

Arrays are the lists that are represented by the square brackets, and the values have commas in between them. They can contain mix data types, i.e., a single array can have strings, Boolean, numbers.

For example:

Example 1: [1, 2, 7.8, 5, 9, 10];

Example 2: [“red”, “yellow”, “green”];

Example 3: [8, “hello”, null, true];

In the above, example 1 is an array of numbers, example 2 is an array of strings, and example 3 is an array of mix data types.

Objects

Objects are JSON dictionaries that are enclosed in curly brackets. In objects, keys and values are separated by a colon ‘:’, pairs are separated by comma. Keys and values can be of any type, but the most common type for the keys is a string.

For example: {“red” : 1, “yellow” : 2, “green” : 3};

Nesting

Nesting involves keeping the arrays and objects inside of each other. We can put the arrays inside objects, objects inside arrays, arrays inside arrays, etc. We can say that json file is a big object with lots of objects and arrays inside.

Example:

In the above code, the song starts with a curly bracket. Therefore, a song is an object. It contains three key-value pairs wherein title, artist and musicians are the keys.’

What is XML?

XML stands for an extensible markup language. It is like HTML, where HTML stands for Hypertext Markup language. HTML is used for creating websites, whereas XML can be used for any kind of structured data.

XML has two ways of handling data, i.e., Tags and Attributes. The tags work as HTML. The start tags start with the <_> and end with the </_>. The start and end tags must match. The names must only be letters, numbers, and underscore, and the tag name must start with a letter only.

For example:

<title> Hello World </title>

Nested Tags

When we put the tag inside of another tag that creates the nested data.

For example:

As we can observe in the above code that inside the color tag, we have three more tags, i.e., red, yellow, and green.

Similarities between the json and XML.

  • Self-describing: Both json and xml are self-describing as both xml data and json data are human-readable text.
  • Hierarchical: Both json and xml support hierarchical structure. Here hierarchical means that the values within values.
  • Data interchange format: JSON and XML can be used as data interchange formats by many different programming languages.
  • Parse: Both the formats can be easily parsed.
  • Retrieve: Both formats can be retrieved by using HTTP requests. The methods used for retrieving the data are GET, PUT, POST.

Differences between the json and XML.

JSON vs XML

The following are the differences between the json and xml:

JSON XML
JSON stands for javascript object notation. XML stands for an extensible markup language.
The extension of json file is .json. The extension of xml file is .xml.
The internet media type is application/json. The internet media type is application/xml or text/xml.
The type of format in JSON is data interchange. The type of format in XML is a markup language.
It is extended from javascript. It is extended from SGML.
It is open source means that we do not have to pay anything to use JSON. It is also open source.
The object created in JSON has some type. XML data does not have any type.
The data types supported by JSON are strings, numbers, Booleans, null, array. XML data is in a string format.
It does not have any capacity to display the data. XML is a markup language, so it has the capacity to display the content.
JSON has no tags. XML data is represented in tags, i.e., start tag and end tag.
XML file is larger. If we want to represent the data in XML then it would create a larger file as compared to JSON.
JSON is quicker to read and write. XML file takes time to read and write because the learning curve is higher.
JSON can use arrays to represent the data. XML does not contain the concept of arrays.
It can be parsed by a standard javascript function.It has to be parsed before use. XML data which is used to interchange the data, must be parsed with respective to their programming language to use that.
It can be easily parsed and little bit code is required to parse the data. It is difficult to parse.
File size is smaller as compared to XML. File size is larger.
JSON is data-oriented. XML is document-oriented.
It is less secure than XML. It is more secure than JSON.

Next TopicJSON Example

You may also like