Difference between JSON and XML
Definition
XML and JSON are both designed to form a simple and standartized way of describing different kinds of hierarchical data structures and to facilitate their transportation and consumption in a standartized way. Nonetheless, there are few substantial differences that deserve attention.
The major differences
Take a look at the following example:
JSON :
{ "employees": [ { "name": "John Crichton", "gender": "male" }, { "name": "Aeryn Sun", "gender": "female" } ] }
XML :
<employees> <employee> <name>John Crichton</name> <gender>male</gender> </employee> <employee> <name>Aeryn Sun</name> <gender>female</gender> </employee> </employees>
Clearly XML is somehow easier to understand for a human. That’s why it’s commonly used for configuration files (although lambdas and fluent APIs are recently emerging as a way of configuration). JSON, on ther other hand, is harder to read, but only from a human perspective.
Aside from that, XML is also a markup language (as the name suggests) and is therefore suitable for document description – the hierarchical elements can also have attributes, which is not present in JSON. On ther other hand, JSON has a very concise syntax for defining lists of elements, which makes it preferrable for text format object serialization.
XML is also the usual choice of base when it comes to Domain Specific Languages.
JSON and JavaScript
JSON's succinct syntax makes it very light and compact. It is also easier to parse from JavaScript, and that’s one of the main reasons it’s preferred for browser-based client-side applications (note that JSON stands for JavaScript Object Notation, it’s just natural). If you want to work with XML from JavaScript, you'll need to use an extra library for that. With JSON, you can just parse the message (let’s say with JSON.Parse() ) and work directly with js objects.
For example:
JSON.parse('{"name":"John"}');
Good :) very clearly explained about the difference between xml and json :)
Glad you like it. ;)
Nice job! This post is simple and clear.
Wanted a quick explanation of the differences … and that's what I got from your article – thanks !
No problem. ;)
I don’t think the article is correct:
– I found the JSON format actually easier to read than XML one.
– Attribute element in principle is not so different than element. In JSON you can have the functionality by using convention.
In practice, I will prefer JSON or YAML instead of XML for configuration file.
An increasing number of javascript plugins now work with json as a data source, and client side MVC frameworks like ember, Knockout, AngularJS, etc use json for internal data modelling. XML is strong on the server side, but for server side data storage, systems like MongoDB and couchDB are moving in on that space too. MongoVUE makes it very easy to work with JSON data stored in MongoDB collections.
It has been a long road for this whole XML versus JSON debate, but you can see where the wind is blowing:
https://www.google.com/trends/explore?q=json%2C+XML#q=json%2C%20XML&cmpt=q
Hi Peter,
Thanks for the additional info. I also think that JSON is slowly advancing, mainly due to some recent JS-related developments, but not only.
Regards
Very nicely represented.. thank you… i would like to share one link where i got nice representation on differenc between JSON and XML
Hey thanks for sharing this info.. While searching this topic i came to this site and another site and i must say very useful.. I m sharing the link , might help you
http://www.nczonline.net/blog/2008/01/09/is-json-better-than-xml/
Thanks for sharing. The security comments on eval() are quite relevant.
This is misleading, because the XML could be written just as simply in this case:
That’s no less readable than the JSON format. That doesn’t make it better or worse, of course, but there’s no need to teach people the wrong way to use XML and then malalign it.
Um, wonderful. I meant this, just with angular bracket
[employees]
[employee name=”John Crichton” gender=”M” /]
[employee name=”Aeyrn Sun” gender=”F” /]
[/employees]
Hi Hogart,
Thanks for your comment. This is my point of view on the matter, which might be or might not be correct.
You are probably right about that specific example, but there are few facts that are undeniable:
1. JSON has less verbose syntax than XML
2. JSON’s syntax requires fewer characters than XML
3. JSON is easier to parse from a browser
For me, XML is generally more intuitive than JSON (I remember that it was my first impression when I was younger developer). Now I prefer JSON’s syntax, but both technologies have their place depending on the task at hand.
Still, the article is just a simple overview designed to give some initial insights.
Regards
XML – eXtensible Markup Language. A language for organizing arbitrary data.
XML is a markup language for documents containing structured information.
Structured information contains both content words, pictures, etc.
XML is being designed to deliver structured content over the web.
Uses more verbose
JSON – JavaScript Object Notation
JSON is a lightweight data-interchange format
It is a subset of Java script programming, so which can be natively read by JavaScript.
It is language independent
Less verbose – so parsing can be faster
How do prove that it is data-interchange format?
JSON is built on two structures:
A collection of name value pairs. This is realized as an object, record, struct, hash table, dictionary, keyed list and associate array.
An ordered list of values. This is realized as array, vector, sequence and list.
It makes sense that a data format that is interchangeable with programming language is very easy to use.
A more modern way of packaging data that’s often used with AJAX.
As an IT profession / architect who hasn’t been a coder for a number of years, I think the question is really this: do these new standards/languages provide ENOUGH of a benefit to justify having 2-3 very similar “standards”, and therefore diluting interoperability in general (as some systems will use JSON, others XML etc)?
In other words did we all really get better off from something very slightly easier to code, vs. each having to understand new notations, coding libraries etc?
Don’t get me wrong, languages and tools MUST advance and evolve, often disruptively, but what was the killer benefit here which was worth the “softer” pains of creating multiple paths when XML seemed to do the job?
– Genuine question as an architect who hasn’t coded for a long time….
Good short piece on XML and JSON. I would however like to add myself to the list of people who actually had the JSON make more sense than the XML from a human reading standpoint :)
@Juror Number 8
Hail JSON