What is YAML ??
YAML is a human-readable data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain't markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.
In this blog, we'll take you on a journey through the fundamentals of YAML, exploring its syntax, structure, and practical applications.
YAML Syntax
NOTE: INDENTATION IS SUPER IMPORTANT IN YAML LIKE PYTHON
Scalars :
Scalars are individual data elements. They can be strings, numbers, or booleans. Scalars are represented as plain text or enclosed in single or double quotes.
name: Hiren
age: 20
student: true
Mappings :
Mappings are key-value pairs, where each key is followed by a colon ":" and its corresponding value.
person:
name: Hiren Timbadiya
age: 20
contact:
email: hirudon@example.com
phone: {home: 123-456-7890 , work: 987-654-3210}
#! can also use brackets like above and here home and work are
# under the phone like their are its children
- The above Code in JSON looks like this.
{
"person": {
"name": "Hiren Timbadiya",
"age": 20,
"contact": {
"email": "hirutimbadiya@example.com",
"phone": {
"home": "123-456-7890",
"work": "987-654-3210"
}
}
}
}
Indentation and Spacing :
In YAML, indentation is crucial for defining the structure of data. Spaces and tabs are both valid for indentation, but it's essential to be consistent throughout your YAML file. Generally, two spaces or two spaces for each level of indentation is a common convention.
games:
- cricket: Bat and Ball
- hockey: Hockey Stick and Ball
Comments :
You can add comments in YAML files to provide additional context or explanations. Comments start with the "#" symbol.
You cannot add multiline comments in YAML like other languages like Java and Javascript.
# This is a YAML comment
name: Hiren Timbadiya
Write a Single Line in multiple lines :
In YAML, when we encounter the need to write longer text, we can achieve this by using the ">" operator in our code editor. This allows us to break the text into multiple lines for better readability and organization.
#! write a single line in multiple lines
message: >
this will be
in one line
#! Above will be same as
message : this will be one line
Basic Data Types in YAML :
Take a Look at Basic Data Types in YAML.
#? Numbers (Int & Float)
centuries: 76
average: 538.93
#? boolean
winningWC: true
#? explicitly specifying datatype to yml
marks: !!int 100
cgpa: !!float 8.65
pass/fail: !!bool true
college: !!str L.D.C.E
infinite: !!float .inf # .inf for infinite
not a number: .nan
hexa: !!int 0x54
exponentialNumbers: 5.99E67
#? NULL
bottle: !!null NULL # we can also use null , NULL , ~
~: this is a null key
#? Dates & Time
date: 2023-08-06
datetime: 2023-08-06T15:30:00
dateTime: 2023-08-06T03:30:00 PM #! 12 hour clock, predefined UTC timezone
datetimeIST: 2023-08-06T03:30:00 PM+05:30 #! IST TimeZone
ADVANCED YAML
in advanced YAML we will look at some important data types of YAML which are essential to understand.
Sequences :
A sequence in YAML represents an ordered list of items. It is denoted by using a hyphen "-" followed by a space for each item. Sequences allow you to store multiple values under a single key.
student: !!seq
- name
- rollNo
- age
- gender
Sparse Sequence :
A sparse sequence is a variation of a regular sequence that allows for gaps or missing indices. You can use numeric indices for items, but they don't have to be consecutive.
#! some of the keys of the seq will be empty
sparse seq:
- hey
- how
- #empty one
- 10000
Nested Sequence
In YAML, you can nest sequences within sequences, allowing you to create hierarchical structures and multidimensional arrays.
#? Nested Sequence
games:
- cricket:
- bat
- ball
- stumps
- hockey:
- hockey stick
- ball
Map (Mapping) :
A map in YAML is an unordered collection of key-value pairs. It is represented using the key followed by a colon ":" and its corresponding value. Maps are useful for storing configurations and associative data. To explicitly describe it is the map we use !!map keyword given in the example.
person: !!map
name: Hiren Timbadiya
age: 20
contact:
email: hirudon@example.com
phone: {home: 123-456-7890 , work: 987-654-3210}
Set :
A set is a YAML datatype that represents an unordered collection of unique items. Each item in the set is represented with a hyphen "-" and space. To explicitly describe it is the map we use !!set keyword given in the example.
#* !!set will allow you to have unique key values
unique example: !!set
? Hiren
? Becky
Ordered Mapping :
An ordered map, also known as OMap, is similar to a regular map but maintains the order of its elements based on insertion. OMap is denoted using exclamation marks like !!omap followed by a space and using the same syntax as a regular map.
#? to use dictionary we use -> !!omap
Movies: !!omap
- RRR:
- Director: S.S. Rajamauli
- Budget: 550 CR₹
- KGF:
- Director: Prashanth Neil
- Budget: 110 CR₹
- Kantara:
- Director: Rishab Shetty
- Budget: 16 CR₹
Anchors and Aliases :
Anchors and aliases in YAML allow you to create references to data within the same YAML document. You can use an ampersand "&" followed by a name to mark an anchor, and then use an asterisk "*" followed by the anchor name to create an alias.
#! reusing some properties we use Anchors
#? to give anchor -> &anchorName
likings: &likes
favourite color: red
favourite game: cricket
favourite player: virat kohli
boy:
name: hiren
<<: *likes #! using above properties
favourite color: pink #! we can also override
Using YAML in Configuration Files :
One of YAML's primary use cases is configuration files for software applications. YAML's readability and simplicity make it a preferred choice for defining settings and options.
database:
host: localhost
port: 5603
username: root
password: s*/675krishna11
Integrating YAML in Programming Languages :
YAML is widely supported across various programming languages. There are libraries and tools available to parse and generate YAML, making it easy to work with YAML in your projects.
Conclusion :
Congratulations! You've completed our beginner's guide to YAML. You now have a solid foundation in YAML syntax, structure, and its practical applications. YAML's simplicity and readability make it an excellent choice for various data representation and configuration needs.
As you continue your journey in the world of programming and development, YAML will prove to be a valuable asset in your toolkit. Experiment with YAML in your projects, and you'll soon discover its power and versatility.
Credits :
Thanks to Kunal Kushwaha 's YAML tutorial you can watch that video to learn about YAML, it is beginner friendly.