Why JSON Is Better Than XML

By Josh Wyse in Developer Posted Aug 27, 2014

Why JSON is better than XML

For awhile, XML (extensible markup language) was the only choice for open data interchange. But over the years there has been a lot of transformation in the world of open data sharing. The more lightweight JSON (Javascript object notation) has become a popular alternative to XML for various reasons. A couple obvious ones are:

  1. Less verbose- XML uses more words than necessary

  2. JSON is faster- Parsing XML software is slow and cumbersome. Many of these DOM manipulation libraries can lead to your applications using large amounts of memory due to the verbosity and cost of parsing large XML files.

XML:

JSON vs. XML

This XML structure is not intuitive, making it hard to represent in code.

JSON:

JSON vs. XML

JSON’s structure is intuitive, making it easy to read and map directly to domain objects in whatever programming language is being used.

Below are more benefits of JSON over XML that may not be as apparent to users:

  1. JSON data model’s structure matches the data: JSON’s data structure is a map whereas XML is a tree. Although a map (just key/value pairs)Screen_Shot_2014-08-21_at_2.19.18_PM can be limiting, that’s what we want, because it is easier to interpret and is predictable.

  1. In code: Items are represented the same way in code. In many languages, especially dynamic ones, you can just ‘slurp in the JSON’ and you immediately have your domain object. It is easy to go from objects in JSON to the objects in code because they align. When going from objects in XML to objects in code they do not align and there is a lot of room for interpretation.

  1. JSON is limiting, but that’s a good thing: JSON is limited in terms of what objects can be modeled. Some may think XML is better because more objects can be modeled and it doesn’t prohibit developers. But even though JSON prohibits developers, it is in a positive way, making the code simpler, more predictable, and easy to read. XML can be formatted to look and function any way a company wants, but it makes it difficult for developers to read, understand, and convert.  In most cases people believe XML is better because developers can do anything under the sun but in the age of simplifying, less is more, making JSON a better alternative.

Building RESTful APIs as Cloud Elements does, requires a reliable, fast, and easy to use form of data exchange. All of our APIs use JSON, and for endpoints that don’t support JSON, we transform our easy-to-understand JSON into XML and back, so you don’t have to deal with it. As RESTful API practices and simpler forms of data exchange become more popular, JSON will be leaving XML practices in the dust.

To learn more from Cloud Elements, check out these suggested resources:

Or check out this free eBook which gives you everything you need to know about building awesome API integrations:

the-definitive-guide-to-API-Integrations-eBook