XML and JSON

JSON vs XML in 2020: Comparison, Features & Example

Posted in XML, JSON
JSON vs XML in 2020: Comparison, Features & Example

XML (Extensible Markup Language) has been around for more than 3 decades now and it is an integral part of every web application. Be it a configuration file, mapping document, or a schema definition, XML made life easier for data interchange by giving a clear structure to data and helping in dynamic configuration and loading of variables!

JSON stores all of its data in a map format (key/value pairs) that was neat and easier to comprehend. JSON is said to be slowly replacing XML because of several benefits like ease of data modeling or mapping directly to domain objects, more predictability and easy to understand the structure. A lot of developers who have used both do not completely agree with this though! Through this article, you will get to know the difference between JSON and XML in a detailed way.

Difference Between JSON and XML

For starters, JSON is just a data format whereas XML is a markup language. You can actually place a query and get your answer through XPath. Similarly, metadata, attributes, and namespaces can be added in XML. Further, XML along with XSL, XSD, XQuery, etc. makes for a powerful combination. These are some important features that still set XML a class apart.

In any case, if a project requires document markup and metadata information, it is better to use XML, otherwise for a more organized data interchange JSON could be your preferred choice.

What is JSON? 

A better option for XML for data transfers, JSON (JavaScript Object Notation) definitely requires less coding and has a smaller size, making it faster to process and transmit data. Moreover, though it is written in JavaScript, it is language-independent. But, that’s all JSON can do. It doesn’t have any of the powerful validation and schema related features that XML has.

So, what makes JSON so popular?

With the popularity of REST APIs over the SOAP/WSDL model, JSON gained momentum in the programming of API code and web services. It is text-based, light-weight and has an easy to parse data format requiring no additional code for parsing. JSON thus helps in faster data interchange and web service results. So, for web services, that need to just return a lot of data and display the same, the ideal choice is JSON!

History of JSON

The data format specified first by Douglas Crockford in March 2001, real-time. Server-to-browser communication protocol without using browser plugins like Flash or Java applets. Its official website launched in 2002, and by 2005 Yahoo started offering its web services in JSON. It soon became an ECMA international standard by 2013. Finally, the most updated JSON format standard was published in 2017. 

Features of JSON

  • Usage is straightforward:  JSON API offers a high-level facade, thus simplifying commonly used use-cases.‬ ‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬
  • Better Performance:  JSON is quite fast as it consumes very little memory space suitable for large object graphs or systems.
  • It is Free: JSON library is open source and free to use for everyone.
  • No need to create a mapping: Jackson API provides default mapping for many objects to be serialized.
  • Clean JSON: Creates clean, and compatible JSON result that is easy to read.
  • No Dependency:  The JSON library does not require any other library for processing.

Pros of JSON

  • It supports all browsers. 
  • Easy to grasp.
  • Its syntax is straightforward. 
  • Can be parsed in JavaScript using eval() function
  • Creation and manipulation are easy. 
  • All major JavaScript frameworks offer support JSON.
  •  Most backend technologies support JSON. 
  • Transmission and serialization of the structured data are done using a network connection.

Cons of JSON

  • JSON offers poor extensibility as no namespace support.
  • Limited development tools support.
  • It provides support for formal grammar definition. 

What is XML?

XML (Extensible Markup Language) is the default choice for data interchange because virtually every language has a parser for it, be it Java, .net, or any others. It is easy to fetch particular data from XML and there are schemas that can validate the XML. XML is used extensively in today’s ‘e’ world – banking services, online retail stores, integrating industrial systems, etc.

One can put as many different types of information in the XML and it still remains simple. Here are a few common benefits of XML:

  • Create interactive web pages, store and render content data to the user based on processing logic using the XSLT processor.
  • Gives provision to define metadata in a reusable and portable format.
  • More accurate web search results because data is stored inside tags.
  • Makes information exchanges like b2b transactions and b2c transactions easy and more accessible.
  • Several web tasks can be automated making them efficient.

If you have worked extensively with XML, you can appreciate both the merits and demerits of the same. The biggest demerit is the complex parser code using SAX or DOM, which is an additional overhead for developers.

History of XML 

It is an application of SGML. It was compiled by a working group of eleven members who never met face to face; the design was built using a combination of weekly teleconferences and email. Its first draft was published between August and November 1996 at Sun Microsystems, and the first version of XML was released in February 1998. 

Pros of XML

  • With the help of XML, the exchange of data is done quickly between different platforms. So, it makes documents transportable across systems and applications. 
  • XML separates the data from HTML
  • XML simplifies the platform change process.

Cons of XML

  • XML requires a processing application.
  • XML syntax can sometimes be confusing as it is similar to other alternatives. 
  • No intrinsic data type support.
  • The XML syntax is redundant.
  • It doesn't allow the user to create his tags.

Structure of XML vs JSON

Let us see the structure of each to appreciate the above point more :

XML (Extensible Markup Language) JSON (JavaScript Object Notation)
 
 01
 Adam
 Cloud computing </technology >
 Development


{
"employees": 
 [
{
 "id": "01",
 "name": "Adam",
"technology": "Cloud computing",
 "title": "Engineer",
 "team": "Development"
}
}

As we see, the tag structure certainly adds some complexity, while the JSON format seems like reading a simple text!

Moreover, with JavaScript-based frameworks like node JS getting more acceptance by the day, when the front-end code is in JavaScript, getting data in JSON format makes it easy to load data into an object tree.

Another scenario where JSON scores over XML are when you want to export data from a web app to a database. JSON is a perfect file format for such conversions because every programming language is capable of parsing JSON – a truly open standard and interoperable format!

JSON has bigger roles to play in the future as with the advent of cloud computing and APIs, the need for proper storage and transfer of data is huge now and is likely to grow further in the future.

We have seen how both JSON and XML have their own benefits and cons and compared how each can work well in different web application scenarios. Let us summarize the above points as well as understand some more differences that can give you further clarity:

Parameters  XML (Extensible Markup Language) JSON (JavaScript Object Notation)
Language XML is a markup language, not a programming language, that has tags to define elements. JSON is just a format written in JavaScript.
Data Storage XML data is stored as a tree structure. Example –
 2001
 Varsha

 2002
 Akash
Data is stored like a map with key value pairs. Example –
{“employees”: [
 {"id":"2001", “name":"Varsha"}, 
 {"id":"2002", "name":"Akash"}
]}
Processing It can perform processing and formatting documents and objects. It does not do any processing or computation
Speed Bulky and slow in parsing, leading to slower data transmission Very fast as the size of file is considerably small, faster parsing by the JavaScript engine and hence faster transfer of data
Namespaces Support Supports namespaces, comments and metadata There is no provision for namespace, adding comments or writing metadata
Document Size  Document size is bulky and with big files, the tag structure makes it huge and complex to read. Compact and easy to read, no redundant or empty tags or data, making the file look simple.
Array Support Doesn’t support array directly. To be able to use an array, one has to add tags for each item.
science 
maths 
computers 
Supports array which can be accessed as –
x = student.subjects[i];
where “subjects” is an array as –
“subjects”: [“science”, “math”, “computers”]

Data Types Support

Supports many complex data types including charts, images, and other non-primitive data types. JSON supports only strings, numbers, arrays Boolean, and object. Even objects can only contain primitive types.
UTF Support XML supports UTF-8 and UTF-16 encodings. JSON supports UTF as well as ASCII encodings.
Security XML structures are prone to some attacks as external entity expansion and DTD validation are enabled by default. When these are disabled, XML parsers are safer. JSON parsing is safe almost all the time except if JSONP is used, which can lead to Cross-Site Request Forgery (CSRF) attack.
Data Processing Though the X in AJAX stands for XML, because of the tags in XML, a lot of bandwidth is unnecessarily consumed, making AJAX requests slow. As data is serially processed in JSON, using it with AJAX ensures faster processing and hence preferable. Data can be easily manipulated using the eval() method.

Conclusion

As both JSON and XML seem to solve a somewhat similar purpose, the comparison is mostly based on the features and benefits of each. The bottom line is that if you have simple data exchanges that need not bother about semantics and validation, go for JSON. But, even with JSON, it is essential to learn XML and related technologies to do more than just data exchange and fast processing! JSON still has a long way to go before it comes at par with the powers of the ‘old is gold’ XML.

People are also reading:

Ramya Shankar

Ramya Shankar

A cheerful, full of life and vibrant person, I hold a lot of dreams that I want to fulfill on my own. My passion for writing started with small diary entries and travel blogs, after which I have moved on to writing well-researched technical content. I find it fascinating to blend thoughts and research and shape them into something beautiful through my writing. View all posts by the Author

Leave a comment

Your email will not be published
Cancel
andy
andy

Good article but skims over and muddles up a few things...

Article states - "Cons of XML: It doesn't allow the user to create his tags."
err... But that's kinda the whole point of xml, since it is a meta-language, meant for creating new mark up languages, i.e 'tags'.

Article states - "XML separates the data from HTML"
This doesn't make sense. HTML is an application of SGML, a separate thing entirely, and doesn't have much to do with xml directly.
If sgml is html's dad, then xml is akin to html's 'step dad'- even though it's younger.

In the history of xml section, you state that "xml is an application of sgml". This is not true. xml is a subset of sgml, then used to replace sgml as a meta-language (or template),
and is used for creating new flavours of xml (via DTD or other schema).

html is an application of sgml.
xhtml is an application of xml (but influenced by html).
svg is another application of xml, and so on...

sorry to be picky.
xml and json are different tools, with only a partial overlap. I expect they'll co-exist happily for some time.

Cynthia Arnold
Cynthia Arnold

How do I create an XML schema in Excel?

Rodney Burns
Rodney Burns

You can easily convert your Excel file to the XML file and can do vice versa. You can also exchange data between different applications. You can follow the below steps to create an XML schema using Excel-
Turn on the Developers tab.
Create a schema for the XML data that defines the XML file structure. This step will be done outside Excel. The tags represent the column name.
Save the XML code with schema.xml.
Open data-set.xlsx.
Select source from Developers tab under XML which will open the XML source task pane.
Select XML Maps to add XML maps.
XML maps dialogue box will open, select add from there.
Select the schema.xml file that we created in step 2.
Now you can drag the elements from the tree on the left pane onto the worksheet to map the data.
Click export from the Developers tab under XML.
Save the file as data-set.xml and click enter to complete the process.

Stephen Grant
Stephen Grant

Does Excel Open XML files?

Milton Ray
Milton Ray

Yes, Excel can open XML files using below steps-

Search for the Default programs in the search bar or from the Start menu, and select the top one from results.
Now open the Settings app from the Start menu, and select the Apps category. Look for the Default Apps from the left pane.
On the Associate, a file type or protocol with a program page, click the .xml file type, and select the Choose default apps by file type, and pick up the Excel app from the list.

Rosalie Ford
Rosalie Ford

What program is used to open XML files?

Freda Fisher
Freda Fisher

Below is the list of some programs to open XML files-
Microsoft Visual Studio 2013
Bare Bones BBEdit 10
MacroMates TextMate
Microsoft XML Notepad
Altova XMLSpy
Progress Stylus Studio
Liquid Technologies Liquid XML Studio
JAPISoft EditiX
SCREEM
Wattle XMLwriter
ElfData XML Editor

Violet Young
Violet Young

How do I convert an XML file to a readable file?

Van Stevens
Van Stevens

You can follow the below process-

Open the XML file with help of Notepad or using an XML editor.
Click on "Edit" under the File menu and click "Select All." to select all the content in the XML file in one go.
Right-click the highlighted text and select "Copy."
Open Microsoft Word to create a new document and make sure that all the options in Word are set to the defaults.
Click on the "Edit" option under the file menu and select the "Paste Special." option.
Scroll down to select the "HTML Format" option which will paste the XML over to the document in its original format.
Save the document as a .doc file to complete the process.

Bruce Burke
Bruce Burke

Is JSON faster than XML?

Everett Patrick
Everett Patrick

JSON is light-weight as compared to XML and there are some other factors that make JSON as the better alternative for XML. Yes, JSON is faster than XML as XML is more verbose to explain the code better. But parsing an XML software is complicated and time-consuming. But DOM parser manipulates the libraries which make your application consume large memory due to verbosity of the code that incurs the cost for parsing XML files.

Rachel Phelps
Rachel Phelps

How XML is more secure than JSON?

Rose Roberson
Rose Roberson

XML is more secure than JSON because of the following reasons-

XML represents encrypted elements within the standard element tags that allow parsers to parse the encrypted elements during the validation process.
XML allows you to encrypt only the desired elements instead of encrypting the whole document. So that you can include a number of secret data for different recipients within a single document.
You can encrypt different parts of the document with different keys that allows multiple users to decrypt the same file with the data intended to them.

Homer Strickland
Homer Strickland

Do I need to keep XML files?

Erik Sutton
Erik Sutton

Yes, there are many reasons that allow you to keep XML files.

XML allows you to identify the data in a correct way that will be understood by the computer.
XML handles the document consistently without any structural errors as it provides a standardized way to control the type of document structure.
XML provides easy and durable for storing and transmitting the data.
XML provides a common syntax for exchanging information among applications.
You can easily change the XML information via scripting.
You can separate the form from content using XML.

Eric Blair
Eric Blair

Is XML RESTful?

Irving Christensen
Irving Christensen

Yes, you can say that the XML is RESTful. REST follows some methods like GET/PUT/DELETE/POST/OPTIONS which allows CRUD like mentality to create, edit and return the data which is exactly the same with XML. so, we can consider XML as RESTful. Also, XML supports hypermedia type which makes it RESTful.

Brandon Edwards
Brandon Edwards

Can we use XML in REST API?

Benjamin Hawkins
Benjamin Hawkins

RESTful services have some restrictions like it will allow hypermedia. It means that you can only consider service as RESTful only if it provides a response as hypermedia. Hypermedia are MIME media types which contain hyper-linking semantics that creates an application flow. REST API can use XML only if the XML is the hypermedia type.