Comparing XML and JSON: What’s the Difference? | TechWell

Comparing XML and JSON: What’s the Difference?

Developer typing programming language

XML (Extensible Markup Language) and JSON (JavaScript Object Notation) are the two most common formats for data interchange. Although either can be used to receive data from a web server, there are differences that set them apart.

Text-based formats

XML makes use of named tags to describe data, while JSON uses key/value pairs. Both formats are text-based, unlike some that are encoded, such as protocol buffers. Both formats are self-describing and often used to store configuration data in software.

Schema and namespaces

Both XML and JSON are schema-less formats with the option to associate a schema. XML has W3C specification for a Schema, while JSON doesn’t. XML also supports namespaces while JSON doesn’t. Several NoSQL databases that support unstructured data use JSON or binary JSON as the storage format.

Browser support

JSON offers better support for bowser clients, as all browsers support its encoding and decoding. XML is also supported by most browsers, but different browsers provide their own implementation. For example, the XMLHttpRequest object is implemented differently by IE and Mozilla.

Data types

JSON supports fewer data types but covers most types commonly used: string, number, object, array, boolean, and null. JSON doesn’t support any data types for date and time. Another notable omission is support for comments in a JSON file. XML offers a wider selection of data types; in fact, a complete W3C Draft is based on XML schema data types

Web services

One of the most popular types of applications that use these data formats is web services. SOAP web services use XML for messaging or data exchange in request and response. The web service itself is described as a WSDL document, which is again an XML document.

REST is more flexible in terms of data format, as it supports JSON in addition to XML. REST + JSON have replaced SOAP + XML web services to a great extent. 

Because of the advantages of JSON in web services several new JSON-based formats and protocols have been developed, including JSON streaming for stream protocols, JSON-RPC for remote procedure call, and BSON for binary JSON. A markup language called JsonML has also been developed to map between XML and JSON.

W3C standards support

XML has several W3C standards, including XML Query, XPath, XML Schema, XSLT, and XForms. JSON doesn’t have many W3C standards, although it does have JSON-LD for linked data.

Advantages and drawbacks

Data stored or exchanged as JSON is more compact or lightweight than XML, as it has fewer formatting requirements. An XML document must start with an XML processing instruction and be well-formed, meaning all start tags must end with corresponding end tags. 

As JSON has less extraneous information associated with it, it is also easier and faster to parse. XML is verbose, and Document Object Model (DOM) XML parsing APIs must construct the complete DOM structure and navigate the XML tag hierarchy to get data stored by an XML document.

To summarize, JSON is a better choice for data exchange between browser clients and web services or between web servers, for processing data with JavaScript, or for unstructured data. Use XML if you need conformance with an XML Schema and other W3C standards, namespaces, or data that requires a wider selection of data types.

Up Next

About the Author

TechWell Insights To Go

(* Required fields)

Get the latest stories delivered to your inbox every month.