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