5. XML

We can't talk about JSON without mentioning XMLarrow-up-right. XML, or "Extensible Markup Language" is a text-based format for representing structured information, like JSON β€” but different (and a bit more verbose).

XML Syntax

XML is a markup language like HTMLarrow-up-right, but it's more generalized in that it does not use predefined tags. Just like how in a JSON object keys can be called anything, XML tags can also have any name.

XML representing a movie:

<root>
  <id>1</id>
  <genre>Action</genre>
  <title>Iron Man</title>
  <director>Jon Favreau</director>
</root>

The same data in JSON:

{
  "id": "1",
  "genre": "Action",
  "title": "Iron Man",
  "director": "Jon Favreau"
}

XML and JSON both accomplish similar tasks, so which should you use?

XML used to be used for the same things that today JSON is primarily used for. Configuration files, HTTP bodies, and other data-transfer can work with either JSON or XML. Personally, I believe that if JSON works, you should favor it over XML. JSON is:

  • Lighter-weight

  • Easier to read

  • Has better support in most programming languages

There are cases where XML might still be better, or maybe even necessary, but that tends to be the exception rather than the rule.