Skip to main content

YAML Tutorial

YAML (YAML Ain't Markup Language) is a human-readable data serialization language often used for configuration files. It is structured and easy to read, making it a popular choice for defining configurations in software development.


1. Basics of YAML​

Key-Value Pairs​

YAML represents data as key-value pairs, separated by colons:

name: John Doe
age: 30
location: New York

Indentation​

YAML uses indentation (usually two spaces) to define structure. Indentation represents nested data.

person:
name: John Doe
age: 30

2. Lists​

Lists are created using hyphens (-).

Example​

hobbies:
- reading
- hiking
- coding

Nested Lists​

Lists can also contain complex data structures:

people:
- name: John Doe
age: 30
- name: Jane Doe
age: 25

3. Comments​

Use the # symbol to add comments:

# This is a comment
name: John Doe # Inline comment

4. Data Types​

YAML supports strings, numbers, booleans, and null values.

name: "John"      # String
age: 30 # Number
is_active: true # Boolean
middle_name: null # Null

5. Multi-Line Strings​

Multi-line strings can be created using | or >.

Example​

description: |
This is a multi-line
string in YAML.

The | preserves line breaks, while > collapses line breaks into spaces.


6. Anchors and Aliases​

Anchors (&) and aliases (*) allow you to reuse data.

Example​

default_settings: &defaults
theme: dark
notifications: enabled

user_settings:
<<: *defaults
theme: light

In this example, user_settings inherits from default_settings.


Summary​

This tutorial covered YAML basics:

  1. Defining key-value pairs and lists.
  2. Using comments and data types.
  3. Writing multi-line strings.
  4. Reusing data with anchors and aliases.

YAML’s simplicity makes it a great choice for configuration files and data serialization.

Content Review​

The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.

About the Reviewer​

For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.