• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

REST API Tutorial

  • REST
  • JSON
  • Dark Mode
Home / JSON / JSON vs XML

JSON vs XML

XML has enjoyed an excellent time for being the only choice for open data interchange. The presence of SOAP APIs as major endpoint applications also contributed to its popularity.

Later, when the REST APIs emerged as a less complicated and equally capable alternative of SOAP, people also started talking about other mediums for data interchange other than XML. That’s where JSON began to gain momentum.

JSON and XML, both have well-documented open standards on the Web (RFC 7159, RFC 4825), and both are human and machine-readable. Neither one is absolutely superior to the other, as each is better suited for different use cases.

Here, I am not focusing on minor syntax related differences. Instead, I am focusing on major decision points while designing web APIs and their capabilities.

JSON advantages over XML

  1. Less verbose

    JSON is significantly less verbose than XML because XML necessitates opening and closing tags and JSON uses name/value pairs concisely delineated by “{“ and “}” for objects, “[“ and “]” for arrays, “,” to separate pairs, and “:” to separate name from value.

  2. Less size

    With the same amount of information, JSON is almost always significantly smaller, which leads to faster transmission and processing. Also, it is noticed that JSON is serialized and deserialized drastically faster than XML.

  3. Close of javascript

    Perhaps the most significant advantage that JSON has over XML is that JSON is a subset of JavaScript, so code to parse and package it fits very naturally into JavaScript code.

XML advantages over JSON

  1. Meta data support

    One of the most significant advantages of XML is that we can put metadata into the tags in the form of attributes. JSON simply lacks this capability. In JSON, attributes will be added as other member fields in data representation which may NOT be desired.

  2. Browser rendering

    Another advantage of XML is that most browsers render it in a highly readable and organized way. The tree structure of XML lends itself well to this formatting and allows for browsers to let users to collapse individual tree elements naturally. This feature would be particularly useful in debugging.

  3. Mixed content Support

    A good use case of XML is the capability of communication mixed content within the same data payload. This mixed content is differentiated with different markup tags.

    This is not possible in JSON.

Purpose of XML and JSON

XML is a data format, AND it is a language also. It has many powerful features that make it much more than a simple data format for data interchange. e.g., XPath, attributes and namespaces, XML schema and XSLT, etc. All these features have been the main reasons behind XML popularity.

JSON was not designed to have such features, even though some of them are now trying to find their places in the JSON world, e.g., JSONPath.

Simply put, XML’s purpose is a document markup. Always prefer to use XML, whenever document markup and meta-data is an essential part of data and cannot be taken away.

JSON’s purpose is structured data interchange. It serves this purpose by directly representing objects, arrays, numbers, strings, and booleans. When meta-data and document markup is not a requirement, always use JSON.

Was this article helpful?

Share this:

  • Twitter
  • Facebook
Previous Tutorial:
What is JSON
Next Tutorial:
JSON Syntax

Reader Interactions

Comments

  1. Dimi says

    June 12, 2020 at 2:07 pm

    JSON-LD allows for JSON to include metadata.

    Reply
  2. Dan says

    April 28, 2020 at 12:34 am

    Another advantage of XML is what the “X” stands for — “extensible”. For example it’s possible to add (and to lesser extent remove) fields from an XML standard and the applications built on older versions of the standard are likely to continue to function (see “progressive enhancement”). One can think of SVG and Excel as examples of such standards. Nonetheless, unless you have good reasons to choose XML over JSON for your standard, JSON is the clear default choice.

    Reply
  3. Nicolas Copia says

    June 1, 2018 at 1:29 am

    Typo in this sentence: “One of the biggest advantage of XML id that you can put metadata into the tags in the form of attributes. ” should be “is that you”

    Reply
    • Laura says

      May 31, 2019 at 4:48 am

      ok

      Reply

Leave a Reply to Dan Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Search Tutorials

JSON Tutorial

  • What is JSON
  • JSON Syntax
  • JSON Data Types
  • JSON Schema
  • JSON Object
  • JSON Array
  • JSON parse()
  • JSON stringify()
  • JSON vs XML
  • JSON with Ajax
  • JSONPath

Footer

References

  • The dissertation by Roy Thomas Fielding
  • Uniform Resource Identifier (URI, URL, URN) [RFC 3986]
  • Internet MediaTypes
  • Web Application Description Language (WADL)

Meta Links

  • About
  • Contact Us
  • Privacy Policy

Blogs

  • How To Do In Java

Copyright © 2020 · restfulapi.net · All Rights Reserved. | Sitemap