What's the Difference Between JSON and XML?

Last Updated Aug 17, 2021
Emma Jagger

Engineer, maker, Google alumna, CMU grad

JSON is an acronym for Javascript Object Notation, and XML is an acronym for eXtensible Markup Language. Both formats allow the exchange of text between disparate applications, while still being readable to humans. They are not programming languages, but methods of data exchange.

XML was built to solve problems with the early internet, when proprietary and arcane text formats were creating issues as applications tried to communicate across HTTP. XML's radical idea was to "do nothing" and give users the ability to structure and define their own text with XML schema. SOAP APIs used XML to exchange data in a highly structured, language-independent format.

However, as API-based web services grew, SOAP and XML began to feel sluggish. REpresentation State Transfer, or REST APIs, mostly replaced SOAP, and JSON, the preferred text format for REST APIs, replaced XML for high-speed data exchange. (If you want to learn more about the difference between SOAP and REST, check out our article here). What differences caused this massive technological adoption of JSON over XML? To answer that we'll look into the differences between JSON and XML.

What is XML?

If you've ever tried to exchange a nicely typed and formatted document between Microsoft Word and another text program, you probably ended up with a mess of unformatted text globules. Frustrating, right? Now imagine this problem on a worldwide scale, as the new Internet allowed communication across HTTP between different text programs. This is the problem XML solved by "making the internet more usable."

XML Format

Below is an example XML example from W3C's Introduction to XML that displays an XML document. The `note` tag contains the elements `to`, `from`, `heading` and `body`. These are all user-defined tags that will behave consistently, even if the XML file is being processed by JavaScript and delivered with HTTP.  

```xml

<?xml version="1.0" encoding="UTF-8"?>

<note>

 <to>Tove</to>

 <from>Jani</from>

 <heading>Reminder</heading>

 <body>Don't forget me this weekend!</body>

</note>

```

See all that tagging? Keep that in mind as you read about JSON.

What is JSON?

JSON is a lightweight, open-standard data interchange format designed especially for human-readable data interchange. The JSON file is the standard data format in passing requests and responses in REST APIs and web applications. JSON is self-describing. A human or a machine can read JSON data and understand it, and it is less verbose, faster, and more readable than XML. In contrast to XML, JSON supports a compact style to improve its readability. JSON uses key-value pairs to improve extensibility.

JSON Format

JSON is the syntax used in the REST API response when you send a GET request for a record. Here is a curl request and a JSON object:

```curl 'https://holidays.abstractapi.com/v1/?api_key={YOUR API KEY}&country=US&year=2020&month=12&day=25'```

will return a JSON card with your requested information:

```json

[

   {

       "name": "Christmas Day",

       "name_local": "",

       "language": "",

       "description": "",

       "country": "US",

       "location": "United States",

       "type": "National",

       "date": "12/25/2020",

       "date_year": "2020",

       "date_month": "12",

       "date_day": "25",

       "week_day": "Friday"

   }

]

```

This assumes you have `application/json` in the `Content-type` header of your request.

Notice the key-value pairs: `firstName` being a key, and "John" being its value. We also see some carry-over from JavaScript data structures, as the arrays are inside square brackets, and objects are inside curly braces.

  • Data is represented in key/value pairs.
  • Curly braces hold objects and each name is followed by a colon, and key/value pairs are separated by a comma.
  • Square brackets hold arrays and values are separated by a comma.

JSON vs XML

When you compare the XML and JSON examples, notice how little tagging is required by JSON to establish data types- JSON doesn't use end tags. This makes JSON a more efficient method of on-demand data transfer. Where before a programmer would have to know XML and establish an XML schema, JSON is much more lightweight while still being both human and machine readable.

SOAP APIs only use XML, while REST APIs can use JSON, XML, and HTML. There are security advantages to using SOAP and XML, which is why industries like banking and enterprise software choose to continue using them. That's not to say REST and JSON are not secure, but SOAP and XML offer some security features (ws-security, for example) that REST and JSON do not. Finally, XML must be parses by an XML parser, while JSON can be parsed by a standard Java Script function. JSON parses 10x faster than XML.

Conclusion

While this article might make it sound like JSON should replace XML entirely, that is not the case. XML has many uses in web publishing, computing, and data exchange. REST APIs can still exchange XML formatted requests and responses, though JSON is faster due to being more lightweight. In publishing, companies still use XML's Document Type Definition, where different industries can build their own standards with XML (in Document Type Definition, or `.dtd` format), and data within these structures still worked outside of the environment. XML and JSON solve different problems for different times. Where XML solved communication breakdowns in the early days, JSON is geared more specifically for high-speed REST usage. And with gPRC and GraphQL solving the new problems that REST and JSON face, we could be seeing less REST dominance in the future.

If you'd like to learn more about XML, continue with the excellent course at W3C, the Web consortium that initially helped create XML. Besides knowing the history of the language, you'll run into it more often than you think, and it's good to have a handle on it.

Related articles

No items found.
Start using one of Abstract's 10+ API's for free today
Get started