{"id":3449,"date":"2024-01-11T02:15:00","date_gmt":"2024-01-11T07:15:00","guid":{"rendered":"https:\/\/www.both.org\/?p=3449"},"modified":"2024-01-07T21:13:36","modified_gmt":"2024-01-08T02:13:36","slug":"how-yaml-actually-works","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=3449","title":{"rendered":"How YAML actually works"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"3449\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p class=\"has-small-font-size\">Image by: Opensource.com CC-by-SA 4.0<\/p>\n\n\n\n<p>YAML stands for &#8220;YAML Ain&#8217;t Markup Language&#8221; and it&#8217;s described on <a href=\"https:\/\/yaml.org\">YAML.org<\/a> as a human-friendly data serialization language. In other words, it&#8217;s a standardized way to make a list. To ensure that the list you make can be reliably parsed, though, there are strict rules around how YAML is formatted, and that sometimes confuses new YAML users.<\/p>\n\n\n\n<p>The good news is that YAML is essentially governed by just three principles.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Indent your YAML<\/h2>\n\n\n\n<p>The first rule of YAML formatting is that each node in your list must be indented further than its parent node, and all sibling nodes use the same indentation.<\/p>\n\n\n\n<p>For example, suppose you have the item <code>operating system<\/code> in a YAML list. This item contains <code>Linux<\/code> and <code>BSD<\/code>. The child elements (<code>Linux<\/code> and <code>BSD<\/code>) must each be indented to the right more than the <code>operating system<\/code> element, but equal to one another.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\n- operating systems:\n  - Linux\n  - BSD<\/code><\/pre>\n\n\n\n<p>It&#8217;s a common convention to indent in multiples of two, probably because one space is harder to notice at a glance compared to two spaces. It&#8217;s not technically required to indent in multiples of two, but many YAML linters (applications that verify valid YAML) return warnings for indents with odd numbers of spaces.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create a sequence in YAML<\/h2>\n\n\n\n<p>The second principle of YAML is the <em>sequence<\/em> datatype. This is the simplest datatype in YAML, and it&#8217;s basically a fancy term for &#8220;list&#8221;, which is exactly what it constructs. When you list singular items (called a <em>scalar<\/em> in YAML) on a separate line, you create a sequence.<\/p>\n\n\n\n<p>Simple though it may seem, this is a valid YAML sequence:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\nLinux\nBSD\nIllumos<\/code><\/pre>\n\n\n\n<p>It&#8217;s also valid to write it with dashes in front:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\n- Linux\n- BSD\n- Illumos<\/code><\/pre>\n\n\n\n<p>That&#8217;s all a sequence is. There are no child nodes in a sequence, because a sequence is just a list of singular items, one item on each line. For a child node, you need a <em>mapping<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create a mapping in YAML<\/h2>\n\n\n\n<p>The third principle of YAML is the <em>mapping<\/em> datatype. A mapping is a key and value pair. When you want to include a term and its definition in a YAML file, then you use a mapping.<\/p>\n\n\n\n<p>This is a valid mapping in YAML:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\nOS: Linux<\/code><\/pre>\n\n\n\n<p>As you might guess, you can have a sequence of mappings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\nOS: Linux\nCPU: AMD\nRAM: 32G<\/code><\/pre>\n\n\n\n<p>As usual, you can also write this with dashes as bullet points:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\n- OS: Linux\n- CPU: AMD\n- RAM: 32G<\/code><\/pre>\n\n\n\n<p>That&#8217;s not all you can do with a mapping, though. You can also include a sequence within a mapping! You&#8217;ve already seen this in this very article, in fact. My first example of YAML was a mapping containing a sequence:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\noperating systems:\n  - Linux\n  - BSD<\/code><\/pre>\n\n\n\n<p>This maps the values <code>Linux<\/code> and <code>BSD<\/code> to the parent node <code>operating system<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Making YAML simple<\/h2>\n\n\n\n<p>You now know everything you need to know about YAML! You can learn more about further features of the format (such as commenting, line breaks, null nodes, and so on) by reading the official <a href=\"https:\/\/yaml.org\/spec\/1.2.2\/\">YAML specification<\/a>, but what you&#8217;ve learnt in this article may be all you ever need.<\/p>\n\n\n\n<p>In addition to the basics I&#8217;ve provided in this article, there are two general rules to keep in mind:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Use <code>yamllint<\/code> or some kind of linter or validator before committing your YAML to production. A good linter alerts you of suboptimal syntax that could cause some parsers (human or computer) trouble and, most importantly, catches errors that definitely render your YAML unusable.<\/li>\n\n\n\n<li>When in doubt, keep in mind the three principles described in this article! These are the building blocks of YAML: sequence, mapping, and indentation. When a linter throws an error, it means you&#8217;ve violated one of these principles. Look at the offending line and determine whether it&#8217;s a sequence or a mapping, and verify indentation.<\/li>\n<\/ol>\n\n\n\n<p>YAML is designed to be easy, but often when something is simple in technology it&#8217;s because it maintains strict rules that can&#8217;t withstand variation. Don&#8217;t confuse YAML with an arbitrary bullet point list you jot down in a notebook before you go shopping. YAML is a simple database of information, carefully structured for reliable parsing. Learn it once, and write it well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to create YAML correctly from the start.<\/p>\n","protected":false},"author":31,"featured_media":3307,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[5],"tags":[152,97],"class_list":["post-3449","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-programming","tag-sysadmin"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/3449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3449"}],"version-history":[{"count":3,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/3449\/revisions"}],"predecessor-version":[{"id":3463,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/3449\/revisions\/3463"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/3307"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}