{"id":253,"date":"2018-04-30T16:17:43","date_gmt":"2018-04-30T16:17:43","guid":{"rendered":"https:\/\/restfulapi.net\/?p=253"},"modified":"2022-12-30T16:19:15","modified_gmt":"2022-12-30T10:49:15","slug":"json-vs-xml","status":"publish","type":"post","link":"https:\/\/restfulapi.net\/json-vs-xml\/","title":{"rendered":"Difference between JSON and XML"},"content":{"rendered":"\n
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.<\/p>\n\n\n\n
Later, when the REST APIs emerged as a less complicated and equally capable alternative to SOAP, people also started talking about other mediums for data interchange other than XML. That’s where JSON began to gain momentum.<\/p>\n\n\n\n
JSON and XML, both have well-documented open standards on the Web (RFC 7159<\/a>, RFC 4825<\/a>), and both are human and machine-readable. Neither one is absolutely superior to the other, as each is better suited for different use cases.<\/p>\n\n\n\n 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.<\/p>\n\n\n\n JSON is significantly less verbose than XML because XML necessitates opening and closing tags and JSON uses name\/value pairs concisely delineated by \u201c{\u201c and \u201c}\u201d for objects, \u201c[\u201c and \u201c]\u201d for arrays, \u201c,\u201d to separate pairs, and \u201c:\u201d to separate name from the value.<\/p>\n\n\n\n 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.<\/p>\n\n\n\n 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.<\/p>\n\n\n\n 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 that may NOT be desired.<\/p>\n\n\n\n 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 collapse individual tree elements naturally. This feature would be particularly useful in debugging.<\/p>\n\n\n\n A good use case of XML is the capability of communicating mixed content within the same data payload. This mixed content is differentiated with different markup tags.<\/p>\n\n\n\n This is not possible in JSON.<\/p>\n\n\n\n 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.<\/p>\n\n\n\n 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<\/a>.<\/p>\n\n\n\n Simply put, XML\u2019s purpose is a document markup. Always prefer to use XML, whenever document markup and meta-data are an essential part of data and cannot be taken away.<\/p>\n\n\n\n JSON\u2019s 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.<\/p>\n","protected":false},"excerpt":{"rendered":" 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 to SOAP, people also started talking about other mediums for data interchange … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":254,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[17,18],"_links":{"self":[{"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/posts\/253"}],"collection":[{"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/comments?post=253"}],"version-history":[{"count":2,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/posts\/253\/revisions"}],"predecessor-version":[{"id":1347,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/posts\/253\/revisions\/1347"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/media\/254"}],"wp:attachment":[{"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/media?parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/categories?post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/restfulapi.net\/wp-json\/wp\/v2\/tags?post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}1. JSON advantages over XML<\/h2>\n\n\n\n
1.1. Less verbose<\/h4>\n\n\n\n
1.2. Less size<\/h4>\n\n\n\n
1.3. Close of javascript<\/h4>\n\n\n\n
2. XML advantages over JSON<\/h2>\n\n\n\n
2.1. Meta data support<\/h4>\n\n\n\n
2.2. Browser rendering<\/h4>\n\n\n\n
2.3. Mixed content Support<\/h4>\n\n\n\n
3. Purpose of XML and JSON<\/h2>\n\n\n\n