This article will singlehandedly get you to know all about YAML.

What is YAML?

YAML is a data serialization language, meaning it’s used to store data in a more organized, efficient way. It’s very similar to JSON (JavaScript Object Notation) but maybe a bit easier to read for the user.

Designed to be more clean and readable, YAML was originally an acronym for “Yet Another Markup Language”. However, after the developers decided that the goal here was to store data rather than to markup a document, they changed it to “YAML Ain’t a Markup Language”.

A YAML file can be distinguished with the extensions .yml or .yaml.

YAML Basics

# This is a comment

var1: “This is a string”

var2: ‘This is also a string’

var3: null

var4: 50

var5: 37.5

var6: 1e+7

variableDate: 2010:04:12 12:02:21

object1:

var1: “This is a string”

var2: ‘This is also a string’

var3: null

var4: 50

var5: 37.5

var6: 1e+7

Notice: If the format above takes too much space and you would like something more compact, you can also create an object with curly brackets.

object 1: {var1: “This is a string”, var2: ‘This is also a string’, var3: null, var4: 50, var5: 37.5, var6: 1e+7}

listHyphens:

-var1: “aaa”

-var2:50

listSquareBrackets: [var1: “aaa”, var2: 50]

listComplex:

-{var1: “aaa”, var2: 50}

-{var1: “bbb”, var2: 55}

So your text data is huge paragraph and you decided to hit enter to break it into pieces and make it more readable? But it’s still one paragraph, and you want YAML to take it as that, right? No worries. Just use a greater than sign (>) after the variable name and it will ignore the enter, taking the whole thing as a one piece paragraph.

Or maybe you are trying to save this article as data and would like to preserve all the beautiful indentations, all the formatting I’ve made? Just put a straight line (|) after the variable name.

var1: &strawberries “chocolate”

var2: *strawberries

And if you save this, you will find that var2 now holds the value “chocolate”.

Notice: If you would like to anchor not the value alone but an entire variable-value combination, you will need to put the combination inside another variable first (which you can do with the help of indentication). Then, you will have to create a new variable and attain the value with the star sign just like above, but with one difference. You will have to use a double less-than sign as shown below;

var1: &anchorThis

strawberries: “chocolate”

var2:

<<:*anchorThis

And now var2 holds strawberries: “chocolate” as input.

var1: !!float 15

And now, var1 holds the value of not 15 but 15.0, hence the change in the value.

Conclusion

YAML is a language which can help you store and organize data. Although it’s much more clean and user-friendly than it’s competitors, keep in mind that if all else fails you can convert a YAML file into a JSON file!

I write about all that I’m interested in: Tech, Software, Physics, Math, and all development tools!