YAML configs them all
YAML is my favorite for configurations and orchestrations.
I really like YAML much that I can write my work orchestration into YAML files and it’s self-explaining.
What is YAML?
YAML has been mentioned once in my blog here.
YAML formats has been adopted and very popular in recent years from its flexibility and human-readable format. Many popular services support YAML configurations for example, Kubernetes, Docker compose, openAPI, and Github Actions.
Benefits
Practically, YAML is somehow like JSON but it brings some more benefits that I like so much.
Indentation
YAML is indent-based like Python, so the learning curve isn’t high for me.Comments allowed
In JSON, we can’t write comments but YAML is allowing it. I usually add comments to explain what is the purpose of this section.No unnecessary brackets
In JSON, we need to wrap everything in brackets so the file would become bigger from the plenty of brackets and spaces. While YAML can be written without concerning about wrapping.Strings in free-style
In JSON, we have to wrap all strings in quotes ("string"
) but in YAML, we can write them with or without quotes, even a paragraph can be written with a symbol that is pipe (|
) or greater than (>
).
Syntax
YAML must be written in key-value pairs to describe configurations and here is a sample YAML that would be easier to understand its syntax.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
a-key: "a value"
some-int: 1
some-float: 0.01
some-bool: true
some-str: "sample string in a quote"
another-str: sample string without a quote
paragraph: |
a very long string
with new line inside
that use | to read better
another-paragraph: >
a very long string
with new line inside
that use > to read better
array: [1, 2, 3, 4]
another-array:
- 1
- 2
- 3
- 4
# some-comment: "this int-inside-nest a comment"
object:
int-inside-object: 100
object-inside-object:
some-key: some value
object-array:
- index: 1
item: shirt
dimension:
width: 10
height: 20
- index: 2
item: pants
dimension:
width: 15
height: 30
YAML even has more advance features but I don’t use them often such as anchor (&
), alias (*
), and override (<<:
) to refer the values.
1
2
3
4
5
6
7
8
9
10
11
12
server-a:
- name: "computer A"
spec: &spec-default # anchoring as `spec-default`
ram: 8
harddisk: 1024
os: windows
- name: "computer B"
spec: *spec-default # aliasing to `spec-default`
- name: "computer C"
spec:
ram: 4
<<: *spec-default # aliasing to `spec-default` and merge with override (`<<:`), so ram will be 8
And type casting.
1
2
3
4
5
casting:
str-to-int: !!int "123" # result is 123
str-to-float: !!float "123.456" # result is 123.456
int-to-str: !!str 123 # result is "123"
float-to-str: !!str 123.456 # result is "123.456"
Language Servers
This feature is great for me. We can setup a “language server” which help us validate the structure of YAML we are writing by giving its schema and it will show up suggestions, autocompletion, and errors, like this.
We need 2 things to make it work.
Language Server Protocol (LSP)
LSP or Language Server Protocol is a helper for IDEs to communicate with the language server to understand and detect, suggest, and validate the code that we are writing.
We can install the LSP for YAML in VSCode by this plugin: YAML by Red Hat. Or LSP for Neovim via Mason or nvim-lspconfig with the name “yaml-language-server”.Target schema
We have a LSP to help translate then we can tell the translator that we want this schema as a structure of what we are writing. We just add the first line telling so like this:1 2 3
#yaml-language-server: $schema=<url or path/to/schema> yaml-content: ...
We can refer to a URL of the schema or a path of schema file. One of the references I usually look into is this website.

YAML traversal
There is an old blog I wrote about jq
to travel over JSON files. Link below.
Here we have yq
to do the same on YAML files. Like this.
yq 'explode()'
dereferences anchors and aliases.
yq -o=json
exports the object into JSON format.
yq
can be installed via homebrew or other methods. Please visit the link below to find out more information including the syntax of yq
.
Besides, we can work with YAML in popular programming languages such as Python, Go, and JavaScript by using their libraries.
For example:
- Python with
PyYAML
- JavaScript with
js-yaml
- Go with
gopkg.in/yaml.v3