A quick sip of YAML

Rahul Vashishth
WeTheITGuys
Published in
2 min readMar 28, 2019

This blog contains the basics of YAML with short examples. Rules, scalars, object, nested elements, array of scalars, array of objects, null value, anchors and alias, multiple document in one file

  • Yaml is case sensitive
  • you must not use tabs, use space instead
  • Comments are single liner and start with # sign
  • key-value pair. In key-value, the colon must be followed by space
# dart pubspec file
name: my_dart_package
version: 0.1

If you have worked on pom.xml(maven) or package.json(js). its good to practice yaml using yaml to json or yaml to xml converter. https://codebeautify.org/yaml-to-json-xml-csv

  • A single yaml file can have multiple yaml documents. A new yaml document starts with three dashes and ends(optionally) with three dots.
---
name: my_first_app
...
---
name: my_second_app
...
  • Try to keep the indentation as one space. Keep the same number of space for each level.
  • An array of single scalar value
name: my_app
tags:
- dart
- dartlang
- flutter

Equaliant Json of above yaml

{
"name": "my_app",
"tags": [ "dart", "dartlang", "flutter"
]
}
  • Create an array of object pair. Each dash starts a new object
tags:
- name1: dart
value: one
- name2: dartlang
value: two

Equaliant Json of above yaml

{
"tags": [
{
"name1": "dart",
"value": "one"
},
{
"name2": "dartlang",
"value": "two"
}
]
}
  • Define null value. use null or tilde operator
---
address:
city: sikar
street: null
sector: ~
  • Nested elements. using indentation in new line
user:
address1:
city: skikar
address2:
city: gurgaon

Equaliant Json of above yaml

{
"user": {
"address1": {
"city": "skikar"
},
"address2": {
"city": "gurgaon"
}
}
}
  • Anchors and alias AKA reference variables. Create a variable using & and refer it using *
user:
presentAddress:
city: sikar
mobile: &myairtel 9810936551
permanentAddress:
city: ggn
mobile: *myairtel

Equaliant Json of above yaml

{
"user": {
"presentAddress": {
"city": "sikar",
"mobile": 9810936551
},
"permanentAddress": {
"city": "ggn",
"mobile": 9810936551
}
}
}

--

--

Rahul Vashishth
WeTheITGuys

Tech Enthusiast, Cloud, Kubernetes, Java, DevOps. Environmentalist.