Difference Between JSON and XML
Learn via video course
Overview
JSON(JavaScript Object Notation) is a data interchange format, and XML(eXtensible Markup Language) is a custom markup language that is used to interchange data and can do many other things like data validation. Both JSON and XML are used to store and interchange data in an easily understandable format. Almost all programming languages like Python, JavaScript, and Ruby, along with various APIs (Application Programming Interfaces) have parsers for JSON and XML through which data can be interchanged. Even though the purpose of using XML and JSON is the same, they have their ways of operations and can be used in different scenarios.
Scope of Article
- This article gives a brief about JSON and XML files along with their structure.
- History, advantages, and disadvantages of both JSON and XML are covered in this article.
- It compares JSON vs. XML along with the examples.
What Is JSON (JavaScript Object Notation) in XML vs JSON?
- JavaScript Object Notation(JSON) is a data-interchange format. In simple words, JSON is a file format that stores and transmits data objects in human-readable texts.
- It is a lightweight and language-independent(i.e., JSON does not use the syntax of any programming language and is not dependent on any programming language for its functionalities) data interchange format used to send data to various APIs.
- JSON syntax is derived from the javascript programming language, but the code for reading and generating JSON data can be written in any programming language as its language-independent. And it's easy for humans to read and write.
- All the JSON files are saved with the .json extension.
- JSON files can store data with data types string, number, boolean, null, etc.
- JSON uses JSON Objects and JSON Arrays to store and transfer data across the Web.JSON Objects, and JSON Arrays are further explained in this article.
1. JSON Objects:
JSON objects are used to store and transfer data across the web. Objects in JSON store data in key-value pairs. Key-value pairs are related elements where a Key is a constant that defines the data set while a value is a variable of that data set. For eg. "color" can be a key and colors like "blue", "red", etc. can be the values.
Keys in JSON objects should be strings, and values can be of any valid JSON data type. JSON objects are enclosed in curly braces, i.e., {}, while the keys & values are separated by a colon, i.e., :, and each key-value pair is separated by a comma.
Example:
Above is the key-value pair that stores data related to a book. Here, book_name, author, and genre are the keys, and their related values are there in the values part.
2. JSON Arrays:
Arrays in JSON are just like arrays in any other programming language. JSON arrays are a list of objects enclosed in square brackets, i.e., []. Values stored in JSON array can be of data types string, number, object, boolean or null. JSON arrays can be a list of JSON objects which means multiple key-value pairs can be stored in a single array.
Example:
The above array shows the availability of cars in a car showroom. Each element of the array is a JSON object that stores data in the form of key-value pairs. Here, the objects have information about distinct cars like their id, names, and if they are present in the showroom or not.
History of JSON
- JSON format was specified by Douglas Crockford in the early 2000s.
- Official website of JSON was launched in 2003.
- JSON was created to hold the structured data that could be used in javascript. But it became so popular that it is used to transfer data for all kinds of applications.
- Yahoo! was the first company to start offering its web services in JSON in 2005.
- JSON became ECMA international standard in 2013.
- In 2017, the most updated format standard of JSON was published.
Advantages of Using JSON
- Easy to read and write due to simple syntax.
- Supported by all browsers and is recognized by JavaScript.
- Can be used with almost all programming languages and provides support for all browsers.
- Transmission of JSON files over browsers is much faster.
- Due to the increasing popularity of javascript, the use of JSON has been increasing.
- JSON supports different data types and key-value pairs, which makes it easy to understand.
Disadvantages of Using JSON
- JSON does not support namespace and cannot be extended.
- It lacks formatting validation, meaning incorrect data structures can be passed into your APIs.
- JSON is less secure compared to XML.
What Is XML (Extensible markup language) in XML vs JSON?
- XML stands for eXtensible Markup Language that is designed to store data and is popularly used to distribute data across the web and various APIs.
- A markup language is a set of symbols presented such that it is easy for humans and computers to understand.
- XML is designed to store and transfer data, but it is not used to present data.
- It is widely used for data interchange as almost every programming language has a parser for it. And it's easy to fetch and validate data from XML.
- XML has provision to store namespace. A Namespace is a mechanism by which an element and attribute name can be assigned to a group. This makes it easy to differentiate between elements that come from different sources.
- Metadata is data about data. XML allows the storage of metadata in its files that gives information about the source of files or any other metadata. In a way, XML tags can also be considered metadata because they explain what kind of data/attribute they are holding.
- It is used extensively used in today's virtual world, like e-banking, online shopping, etc.
- XML is case-sensitive and uses tags to store data.
- Two ways of handling data in XML are Tags and Attributes.
- Tags in XML are similar to that of HTML. Tags start with <> and end with </>. The names of the start and end tags must match.
Example:
The above example gives us an idea about XML file format and how tags are used in XML. Here <rainbow> is the main tag that has other sub-tags like <violet>, <red>, which display numeric values.
History of XML
- XML is derived from SGML(Standard Generalized Markup Language), which is an international standard for the definition of markup languages.
- SGML/GML was invented by Charles Goldfarb, Ed Mosher, and Ray Lorie in 1970.
- The development of XML started in the year 1996 at Sun Microsystem.
- First version of XML, XML 1.0 was released in February 1998.
Advantages of Using XML
- XML separates data from normal HTML documents.
- It simplifies the platform exchange process.
- Support for different APIs and almost every programming language has XML parser.
- User-defined tags can be created in XML.
- XML does something that JSON cannot do – it can dress it up and allow languages like Java and C++ to create interfaces for applications that may not be web-based.
Disadvantages of Using XML
- The syntax used in XML is complex, and using tags makes it similar to other web-based programming languages, which makes it confusing.
- There is no data-type support in XML.
- XML files are bulky and require more size.
Structure of XML vs JSON
1. Structure of XML
- XML handles data in two ways tags and attributes.
- These tags are case-sensitive.
- Tags in XML are similar to that of HTML. Tags start with <> and end with </>. Names of the start and end tags must match.
- The names of tags must only be letters, numbers, and underscore, and the tag name must start with a letter only.
- Structure:
Above is the structure of XML tags. And it shows the hierarchy of XML tags. XML follows a tree data structure. The tree starts at the root and branches to the lower level of trees. <root> tag is at the root of the tree.
2. Structure of JSON
- JSON handles data in two ways JSON arrays and JSON objects.
- Data structural hierarchy of JSON is in map format. As it handles data in key-value pairs.
- It supports data types like string, number, boolean, array, and null.
- JSON arrays are a list of objects, enclosed in square brackets, i.e., [].
- Object in JSON are dictionaries that store data in key-value pairs. Keys in JSON objects should be string, and values can be of any valid JSON data type.
- JSON objects are enclosed in curly braces, i.e., {}, while the keys & values are separated by a colon, i.e., :, and each key-value pair is separated by a comma.
- Structure:
Here, the student is the array that stores the student's information in the form of key-value pairs.
Similarities between JSON and XML
- Both JSON and XML serve the same purpose of storing and transferring data across the web.
- These are human-readable, easy to understand, and self-describing.
- Both have parsers in most programming languages and APIs. The code in JSON and XML is converted into the code that is understandable by the compiler of the programming language in which it is used.
- The data in both JSON and XML can be fetched from web servers using HTTP requests. HttpRequests is used to request data from servers. The methods used for fetching the data are GET, PUT, and POST.
- JSON and XML support hierarchical structure, i.e., they store values within values.
- Both JSON and XML are open source software, and the software code is publicly accessible anyone can see, modify, and distribute the code.
Differences between JSON and XML
Now let's see XML vs JSON in detail:
JSON | XML |
---|---|
JSON stands for JavaScript Object Notation. | XML stands for eXtensible Markup Language. |
JSON files are stored with .json extension. | XML files are stored with .xml extension. |
It is extended from Javascript. | It is extended from SGML (Standard Generalized Markup Language). |
It is a way of representing objects using key-value pairs. | It is a markup language and uses tag structures to represent data items. |
JSON is object-oriented as it is a text representation of a javascript object. It is basically an object in JavaScript Notation. | XML is document-oriented. After being extended from SGML, XML has retained its original advantage as a document format, and it supports mixed markup needed in document representation. |
Data stored in JSON in the form of arrays and key-value pairs. | XML stores data in the form of tags and attributes. |
JSON files are easy to understand as compared to XML because of their simple syntax. | XML files are difficult to read and interpret as compared to JSON because of the complex tag structure. |
It doesn't support comments. | It supports comments. |
It doesn't support namespaces and metadata. | It supports namespaces and metadata. |
JSON files occupy less size as they don't have tags. | XML documents are bulky and huge in size. And its tag structure makes them look complex. |
JSON is preferred in Browser-Side technologies because its files can be transmitted in less time. | XML is preferred in Server-Side technologies as it gives the provision to validate the data. |
JSON is less verbose and faster due to the simple format and small file size. These features of JSON have made it more popular in recent times. One of the major reasons behind the popularity of JSON is the increasing use of Javascript. | Complex structure of XML makes its parsing slow and requires more memory. This has reduced the popularity of XML these days. But most of the time, it is preferred due to high security and metadata provisions. |
JSON file format is similar to map data structure. The map is similar to key/value pairs and is useful when interpretation and predictability is needed. | XML file format is similar to tree data structure. Means tree representation of data. This makes working on XML a tedious and time-consuming task. |
Conclusion
- JSON and XML have various similarities and differences.
- JSON and XML are both used to transmit data across the web.
- JSON is currently gaining a lot of popularity these days due to its association with JavaScript.
- Being a markup language, XML can define a set of rules and structures to make data interchangeable.
- JSON is preferred due to faster and lightweight files that can be delivered easily through browsers.