Home > General Programming > Difference between JSON and XML

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"}');

 




Like the article ? Share it ! ;)


  1. Lucky
    June 24th, 2013 at 13:40 | #1

    Good :) very clearly explained about the difference between xml and json :)

  2. June 24th, 2013 at 16:35 | #2

    Glad you like it. ;)

  3. Mike C
    July 3rd, 2013 at 06:49 | #3

    Nice job!  This post is simple and clear.

  4. Big Chris
    July 24th, 2013 at 17:15 | #4

    Wanted a quick explanation of the differences … and that's what I got from your article – thanks !

  5. July 24th, 2013 at 17:39 | #5

    No problem. ;) 

  6. July 30th, 2013 at 17:07 | #6

    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.

  7. August 7th, 2013 at 05:05 | #7

    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
     

     

     

  8. August 7th, 2013 at 10:51 | #8

    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

  9. Priti
    October 5th, 2013 at 19:25 | #9

    Very nicely represented.. thank you… i would like to share one link where i got nice representation on differenc between JSON and XML

  10. Maunty
    October 5th, 2013 at 19:29 | #10

    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/

  11. October 7th, 2013 at 21:19 | #11

    Thanks for sharing. The security comments on eval() are quite relevant. 

  12. Hogart
    December 2nd, 2013 at 22:23 | #12

    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.

  13. Hogart
    December 2nd, 2013 at 22:24 | #13

    Um, wonderful. I meant this, just with angular bracket

    [employees]
    [employee name=”John Crichton” gender=”M” /]
    [employee name=”Aeyrn Sun” gender=”F” /]
    [/employees]

  14. December 5th, 2013 at 19:53 | #14

    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

  15. ShanPalaniRam
    August 12th, 2015 at 15:02 | #15

    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.

  16. Juror Number 8
    October 1st, 2015 at 10:51 | #16

    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….

  17. Thomas
    October 7th, 2016 at 21:30 | #17

    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 :)

  18. Goud
    August 17th, 2017 at 15:10 | #18

    @Juror Number 8
    Hail JSON

  1. September 21st, 2016 at 17:37 | #1
  2. May 19th, 2017 at 16:19 | #2


Copyright © Developing the future 2013. Licensed under the CC> BY-NC-ND 3.0 Creative Commons license.       
Audi R8 wallpapers Sony Xperia Z4 Tablet WiFi