Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML and JSON -- Advantages and Disadvantages?

Tags:

json

xml

I recently heard about JavaScript Object Notation (JSON), and after looking it up, it seems like it's becoming rather popular as an alternative to the Extensible Markup Language (XML).

I went on this page for more info, but it seemed more of an XML-bashing page rather than a comparison page. So I thought I should ask here:

What are the benefits of JSON as compared to XML, and why (if at all) should we choose one over the other?

like image 758
user541686 Avatar asked Apr 10 '11 23:04

user541686


People also ask

What are the advantages and disadvantages of XML?

1) It supports Unicode, allowing almost any information in any written human language to be communicated. 2) It can represent common computer science data structures: records, lists, and trees. 3) Its self-documenting format describes structure and field names as well as specific values.

What are the advantages of JSON over XML?

JSON is faster because it is designed specifically for data interchange. JSON encoding is terse, which requires less bytes for transit. JSON parsers are less complex, which requires less processing time and memory overhead. XML is slower, because it is designed for a lot more than just data interchange.

What are the advantages and limitations of JSON over XML?

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. JSON data model's structure matches the data: JSON's data structure is a map whereas XML is a tree.


2 Answers

  • JSON is more compact and can be easily loaded in JavaScript.
  • XML is stricter and has support for schemas and namespaces.

On the face of it JSON seems superior in every way - it's flexible, more compact and in many cases easier to use (especially when working with JavaScript), however it lacks some key features, in particular:

  • Schema support,

I.e. the ability for party A to specify the format of a document, and the ability for party B to check that they are supplying something that matches this format.

This is crucial when passing data between separate systems, where a deviation from the expected format might mean that the data cannot be processed (or worse, is processed incorrectly).

  • Namespace support,

I.e. the ability to mix data intended to be read by multiple sources (or written by multiple sources) in the same document.

An example of this in action is the SOAP protocol - namespaces allow for the separation of the SOAP "Envelope", or "Wrapper" data which is passed alongside the serialised application data. This allows web frameworks process and handle the SOAP Envelope and then pass the body / payload data onto the application.


JSON is very useful when developing a web application where fast, compact and convenient serialisation of data is required, however it's flexible nature is the very thing that makes it less suitable than XML for transferring data between separate systems, or storing data that will be read by 3rd parties.

Perhaps in time these sorts of features will appear in JSON, but for now XML is the dominant format for things like web services and file formats.

like image 189
Justin Avatar answered Oct 02 '22 16:10

Justin


Advantages of JSON

  • Smaller message size
  • More structural information in the document
    • Can easily distinguish between the number 1 and the string "1" as numbers, strings (and Booleans) are represented differently in JSON.
    • Can easily distinguish between single items and collections of size one (using JSON arrays).
  • Easier to represent a null value
  • Easily consumed by JavaScript

Advantages of XML

  • Namespaces allow for sharing of standard structures
  • Better representation for inheritance
  • Standard ways of expressing the structure of the document: XML schema, DTD, etc
  • Parsing standards: DOM, SAX, StAX
  • Standards for querying: XQuery and XPath
  • Standards for transforming a document: XSLT

Draw

  • Human Readable
  • Easy to parse
like image 40
bdoughan Avatar answered Oct 02 '22 18:10

bdoughan