As you can tell its been a long day (and week) so my script is fairly uncreative, however it demonstrates the many finer points of YAML, which YAML does actually stand for YAML Ain’t Markup Language so all of us geeks can laugh that Ain’t ain’t a word. Or ain’t it?
The #1 goal of YAML is being the most human readable of the 3 DevNet Languages.
It has many similarities with JSON such as using Key Pairs, allowing for Integer / String / Boolean Key Values, however there is one very large difference: Indentation matters!
It does not need to be indented exactly as shown above, but all indentation must match and be consistent, where as with XML or JSON that didn’t matter as long as the Tags closed in the proper order for XML or the Brackets closed in the proper order for JSON.
Note in this script I begin it at the top with — which is required at the top to note that it is opening a YAML document, some YAML interpreters do NOT require it, but for testing purposes I would say it is required. Also required at the bottom is the … that notates the YAML Document is now over, and then you can simply put — on the next line to begin a new YAML document / script.
Note there is a Header but I am not entirely sure if it has an official name, that is followed by Key: Value pairs very similar to JSON as it uses Integers / Strings / Boolean as values and Integers are made into “Strings” if they are enclosed in quotes that are called “Collections” or “Groups” which contain the key value pairs.
Now in playing with the code I really only found that Key Pairs within these “Collections” or “Groups” need to be indented properly, however white / null space does seem to be allowed from what I can see using Visual Studio Code:
I put a red star where it actually makes it error out in the line above where the indentation is incorrect, so the file would stop being read at that line (7) because the line below it (8) is not indented properly and will not be read by YAML.
However it odes seem to allow for some null spacing, of course this defeats the whole human readability thing, but thought it worth noting that it seems to allow this and still remain valid though far from optimal.
A quick discussion of the Key Pair Values that is very similar to Python
Or almost exactly like Python.
YAML allows for the following Value types:
- Integers – Numbers not enclosed in quotes
- Floats – Integers with a Decimal value like 1.23
- Strings – Text value note enclosed in quotes
- Quoted Strings – Text value enclosed in quotes (for a reason shown below)
- Boolean – “true” or “false” binary values
The String Values get a little tricky if you haven’t looked at my posts regarding Python3 Network Programming or done this yourself, as YAML will accept a value of \n to basically act as hitting the return key on a string value within quotes:
^^^ This is an imagine from my Python Scripting, I would use \n to “write” the command to the CLI and then do a return to write the next line, in YAML this can also be done:
This would print out as two lines if called out by a Python script, however that would require importing yaml library / functions into Python that are a bit above my current skillset (off the top of my head), so you will just need to to take my word it would print:
What a Beautiful Day!
Without the ” ” in using the \n it would print it as part of the string, however we can still separate printed lines without ” ” by formatting the string with a null space:
This would not print the NULL Space in the print out, however it will print two different lines, the first will include the \n as part of the first line “Hello World!\n” in the output because the lack of quotes – However you can format YAML in this make your own separate lines rather than using the \n method with closed quote strings.
One important / odd note with YAML – Its Boolean values do not have to be lower case, then can be true / True / TRUE / false / False / FALSE and it will be correct!
That is all there really is to YAML, not a whole lot as its a very easy language
There are just a few little distinct differences from other data formats like the Boolean Value not being lower case only, and allowing for floats within its data formatting.
I will do a recap of all three data format types quick and that will wrap up my review of data formats such as their strengths / weaknesses / goals or case use for them all.
Until Next Time!!!