Has JSON Overtaken XML

We are finding most of data transmissions are now using JSON format data for sending and receiving data, let’s know how the JSON is better than other formats of data and also compare with XML; how it is better, and also the benefits of using JSON.

This document is made simple for helping you to understand about JSON and its benefits in a short time, hope you enjoy reading this article.

What is JSON?


JSON stands for JavaScript Object Notation.


JSON is a lightweight data-interchange format and it is language independent also known for its "self-describing" attribute, and easy to understand.

JSON Sample

  1. {  
  2.     "employees": [{  
  3.         "firstName": "Ipsita",  
  4.         "lastName": "Sethi"  
  5.     }, {  
  6.         "firstName": "Hrithik",  
  7.         "lastName": "Bala"  
  8.     }, {  
  9.         "firstName": "Bhuvan",  
  10.         "lastName": "Mohan"  
  11.     }]  
  12. }  
What are the advantages of JSON over XML?
  • JSON is a valid subset of JavaScript, Python, and YAML.
  • JSON parsing is generally faster than XML parsing.
  • JSON is a more compact format, meaning it weighs far less on the wire than the more verbose XML.
  • JSON is easier to work with in some languages (such as JavaScript, python, and php).
  • Formatted JSON is generally easier to read than formatted XML.
  • JSON specifies how to represent complex datatypes, there is no single best way to represent a data structure in XML.
Say for example, go through the below details

Consider the sample JSON object { "foo": { "bar": "baz" } } could be represented in XML as <foo bar="baz" .> or <foo><bar>baz</bar></baz> or <object name="foo">< property name="bar">baz</property ></object>

Comparing JSON and XML
  1. JSON can contain Integer, String, List, Arrays. XML is just nodes and Elements that needs to be parsed into Integer, String and so on before it is used by your application.

  2. JSON is smaller, faster and lightweight compared to XML. So for data delivery between servers and browsers, JSON is a better choice.



  3. JSON is best for use of data in web applications from web services because of JavaScript which supports JSON. The overhead of parsing XML nodes is more compare to a quick look up in JSON.

  4. For a newbie, JSON might look complex to read and understand because of its structure unless it is well written with all brackets and commas properly indented. 

  5. JSON can be mapped more easily into object oriented system.

  6. JSON and XML both use Unicode and thus support internationalization.

  7. JSON is a better data exchange format. XML is a better document exchange format.

  8. JSON is less secure because of absence of JSON parser in browser. 

  9. If the data is in XML, you can write an XSLT template and run it over the XML to output the data into another format: HTML, SVG, plain text, comma-delimited, even JSON. When you have data in JSON, it’s pretty much stuck there. There’s no easy way to change it into another data format. So here, XML scores over JSON.

    JSON is very useful when developing a web application where fast, compact and convenient serialisation of data is required; however its flexible nature is the very thing that makes it less suitable than XML for transferring data between separate systems.
Simplicity

XML is simpler than SGML, but JSON is much simpler than XML. JSON has a much smaller grammar and maps more directly onto the data structures used in modern programming languages.

Extensibility

JSON is not extensible because it does not need to be. JSON is not a document mark-up language, so it is not necessary to define new tags or attributes to represent data in it.

Interoperability

JSON has the same interoperability potential as XML.

Openness

JSON is at least as open as XML, perhaps more so because it is not in the center of corporate/political standardization struggles.



XML is human readable
  • JSON is much easier for human to read than XML. It is easier to write, too. It is also easier for machines to read and write.

  • XML can be used as an exchange format to enable users to move their data between similar applications.

  • The same is true for JSON.

  • XML provides a structure to data so that it is richer in information.

  • It’s same for JSON too.

  • XML is easily processed because the structure of the data is simple and standard.

  • JSON is processed more easily because its structure is simpler.

  • There is a wide range of reusable software available to programmers to handle XML so they don't have to re-invent code.

  • JSON, being a simpler notation, needs much less specialized software. In the languages JavaScript and Python, the JSON notation is built into the programming language; no additional software is needed at all. In other languages, only a small amount of JSON-specific code is necessary. For example, a package of three simple classes that makes JSON available to Java is available for free from JSON.org.

  • XML separates the presentation of data from the structure of that data.

  • XML requires translating the structure of the data into a document structure. This mapping can be complicated. JSON structures are based on arrays and records. That is what data is made of. XML structures are based on elements (which can be nested), attributes (which cannot), raw content text, entities, DTDs, and other meta structures.
Common exchange format

JSON is a better data exchange format. XML is a better document exchange format. Use the right tool for the right job.

Many views of the one data

JSON does not provide any display capabilities because it is not a document mark-up language.

Self-Describing Data

XML and JSON have this in common.

Complete integration of all traditional databases and formats

(Statements about XML are sometimes prone to a bit of hyperbole.) XML documents can contain any imaginable data type - from classical data like text and numbers, or multimedia objects such as sounds, to active formats like Java applets or ActiveX components.

JSON does not have a <[CDATA[]]> feature, so it is not well suited to act as a carrier of sounds or images or other large binary payloads. JSON is optimized for data. Besides, delivering executable programs in a data-interchange system could introduce dangerous security problems.

Internationalization

XML and JSON both use Unicode.

Open and extensible

XML’s one-of-a-kind open structure allows you to add other state-of-the-art elements when needed. This means that you can always adapt your system to embrace industry-specific vocabulary.

Those vocabularies can be automatically converted to JSON, making migration from XML to JSON very straightforward.

XML is easily readable by both humans and machines
  • JSON is easier to read for both humans and machines.

  • XML is object-oriented.

  • Actually, XML is document-oriented. JSON is data-oriented. JSON can be mapped more easily to object-oriented systems.

  • XML is being widely adopted by the computer industry.

  • JSON is just beginning to become known. Its simplicity and the ease of converting XML to JSON make JSON ultimately more adoptable.
Benefits of JSON

Syntax

JSON's syntax is quite light and definitely less verbose than XML, with all of its start and end tags. When it comes down to pure byte-size, JSON can represent the same data as XML using fewer characters.

Weight

Since JSON syntax requires fewer characters, it is lighter on the wire than XML. The question is if this really matters. Any large data set is going to be large regardless of the data format you use. Add to that the fact that most servers’ gzip or otherwise compress content before sending it out, the difference between gzipped JSON and gzipped XML isn’t nearly as drastic as the difference between standard JSON and XML.

Browser Parsing

On the browser, there’s no such thing as a JSON parser. However, there is eval(), which interprets JavaScript code and returns the result. Since JSON syntax is a subset of JavaScript syntax, a JSON string can be evaluated using eval() and quickly turned into an object that is easy to work with and manipulate. XML parsing on the browser is spotty at best. Each browser implements some different way of dealing with XML and none of them are terribly efficient. In the end, the XML ends up as a DOM document that must be navigated to retrieve data. Native JavaScript objects are much easier to work with in JavaScript than DOM documents, although the playing field would level out considerably if every browser supported ECMAScript for XML (E4X), which makes working with XML data as easy as working with JavaScript objects.

Server Parsing

On the server, parsing is just about equal between JSON and XML. Most server-side frameworks have XML parsing capabilities and many now are starting to add JSON parsing capabilities as well. On the server, these parsers are essentially equal, parsing a text format into an object model. JSON holds no advantage over XML in this realm.

Querying

This is where XML really shines. Using XPath, it’s possible to get direct access to a part of multiple parts of an XML data structure; no such interface exists for JSON. To get data from a JSON structure, you must know exactly where it is or else iterate over everything until you find it.

Format Changes

You have your data in one format but you want it in another. If the data is in XML, you can write an XSLT template and run it over the XML to output the data into another format: HTML, SVG, plain text, comma-delimited, even JSON. XSLT support in browsers is pretty good, even offering JavaScript-level access to it. XSLT support on the server is excellent. When you have data in JSON, it’s pretty much stuck there. There’s no easy way to change it into another data format. Of course it’s possible to walk the structure and do with it as you please, but the built-in support isn’t there as it is with XSLT for XML.

Security

Since the only way to parse JSON into JavaScript objects is to use eval(), it opens up a huge security hole. The eval() function will execute any arbitrary JavaScript code and so is dangerous to use in production systems. Invalid JSON that contains valid JavaScript code could execute and wreak havoc on an application. The solution, of course, is to build a true JSON parser for browsers, but we’re not there yet. On the other hand, XML data is completely safe. There is never a possibility that parsing XML data will result in code being executed.

Conclusion

XML has done a lot for software for data transmission by its easy and readable format, JSON has not fully replaced XML, however JSON has its own features but can’t replace XML where loads of XML data still exist in this world.

Up Next
    Ebook Download
    View all
    Learn
    View all