JSON, YAML, XML... What the hell?!

I recently had an introductory job interview with a small start-up company in Hamburg. During the interview, I was asked a few technical questions. And, well, I stumbled and stuttered. So, after the interview was finished, I did some digging in order to check and expand my understanding.

One of the questions was about object-oriented file formats used for data transfer. There is the popular and widely used JSON format, but what are examples of other formats that serve the same purpose? Honestly, I had no idea.

The founders of this small company called Heisenware, have a podcast on Spotify, explaining Software and IT concepts to novices in the field. Episode #22 deals with this exact topic and so I listened to learn more. I should add, that the podcast is in German, so here comes my, much shortened, summary for English speakers and readers of this blog.

General info about these files

One of the main functions of all three file types, JSON, YAML, and XML, is the transfer of data, for example from a server to a browser. The file type specifies what structure the data are wrapped in and then transmitted and this can have a bearing on how efficient the transfer is. If there is a lot of added syntax in the file, in addition to the data, then the transfer tends to be less efficient. So the idea is to make the syntax as simple as possible in order to increase the efficiency of the data transfer.

JSON

The JSON format is the most commonly used format. The acronym stands for JavaScript Object Notation, and it follows the syntax of an object in the JavaScript programming language, by enclosing data as key and value pairs inside curly brackets.

{
  name: 'Margherita',
  size: 'medium',
  isVegetarian: true
}

In this object notation, it can be directly imported, read, and used in code. The syntax in the file, besides the data themselves, uses only brackets, commas, colons, and quotation marks, making it quite efficient.

YAML

YAML is also a format that is very light on syntax and therefore quite efficient. JSON and YAML files can be translated from one to the other with relative ease.

---
# Sample yaml file
company: spacelift
domain:
 - devops
 - devsecops
tutorial:
  - yaml:
      name: "YAML Ain't Markup Language"
      type: awesome
      born: 2001
  - json:
      name: JavaScript Object Notation
      type: great
      born: 2001
  - xml:
      name: Extensible Markup Language
      type: good
      born: 1996
author: omkarbirade
published: true

A YAML file can do away with brackets and even quotation marks are not necessary. It does this by using indentation and defaulting all entries that contain letters to strings and all entries that consist of numbers to numbers. You are able to force a number to be a string by enclosing it in quotation marks and thus overwriting the default interpretation of the YAML syntax.

According to Wikipedia the acronym YAML originally comes from the tongue-in-cheek phrase 'Yet Another Markup Language' because it was initially developed in a time when many markup languages were developed. But later the acronym was repurposed to stand for YAML Ain't Markup Language to clarify its data-oriented purpose rather than document markup.

XML

XML stands for Extensible Markup Language and when you open up an XML file it might remind you a little bit of an HTML file. You will see a lot of syntax! This makes it not nearly as readable as the JSON and YAML files and it also makes it a lot more syntax-heavy and thus less efficient for data transfer compared to the other two file types.

<CATALOG>
<PLANT>
<COMMON>Bloodroot</COMMON>
<BOTANICAL>Sanguinaria canadensis</BOTANICAL>
<ZONE>4</ZONE>
<LIGHT>Mostly Shady</LIGHT>
<PRICE>$2.44</PRICE>
<AVAILABILITY>031599</AVAILABILITY>
</PLANT>
<PLANT>
<COMMON>Columbine</COMMON>
<BOTANICAL>Aquilegia canadensis</BOTANICAL>
<ZONE>3</ZONE>
<LIGHT>Mostly Shady</LIGHT>
<PRICE>$9.37</PRICE>
<AVAILABILITY>030699</AVAILABILITY>
</PLANT>
</CATALOG>

XML may also contain information about the intended formatting of the file. So for example, the Microsoft Word document format .docx is in actual fact a zip file that contains a .xml file, and with it comes all the formatting information of the Word document contained in the file.

JSON and YAML files contain no formatting information, as they are intended only to contain data which can then be formatted and displayed in many different ways using for example HTML and CSS when the data is loaded into a browser.

Summary

So there you have it! A very short and rudimentary summary of three important file types, JSON, YAML, and XML, used to 'package' data with the specific purpose of data transfer. The less syntax in the file the more efficient the transfer. I should also add, that these file types are generally intended for transferring relatively small amounts of data. The infrastructure for transferring large amounts of data warrants another investigation on my part.

For another short article that also talks about the circumstances in which you might use each of these file types, check out this story on Medium.

Next time I get asked this question, I'll know the answer and so will you!

Thanks for reading!!

Code Block Sources:

YAML Tutorial

6 Ways to create a JavaScript Object

XML Examples

Spotify Episode Link

#22 JSON | YAML | XML | Datenformate | Structurierter Datenaustausch

XmlYamlData TransferJson
Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.