YAML Basic
I always wondered what YAML files were. As a programmer, I’ve seen them frequently, and after some research, I finally understand them. Programmers don’t usually write YAML directly, but it’s incredibly useful to know how they work so you can understand your configuration files.
What is YAML?
- Human-friendly Data: YAML (originally Yet Another Markup Language, but now stands for YAML Ain’t Markup Language) is designed to be a data serialization language that’s exceptionally easy for humans to read and write.
- Configuration Files and More: It’s frequently used for configuration files, but its capabilities go beyond that. YAML can represent complex data structures, making it suitable for various data-related tasks.
Key Features
- Readability: YAML’s core strength is its readability. It emphasizes a clean syntax that avoids excessive punctuation and symbols.
- Indentation-Based Structure: Unlike JSON or XML, YAML uses indentation (SPACE, NOT TABS) to define the structure of data. This makes it visually intuitive.
- Superset of JSON: If you know JSON, you already understand a good chunk of YAML.
- Data Types: YAML supports common data types:
- Scalars (strings, numbers, booleans)
- Lists (sequences, similar to arrays)
- Dictionaries (key-value pairs, also called maps or hashes)
Note: .yml
and .yaml
mean the same thing. Comments are made using #
.
Example
Here’s a simple example of a YAML configuration file that demonstrates common YAML features:
person:
name: "Mike"
occupation: 'Programmer'
age: 23
fav_num: 1e+10 # Exponential number
male: true # Boolean value
birthday: 2001-12-25 # ISO 8601 date format
hobbies:
- Hiking
- Movies
movie:
- "Source Code"
- "" # An empty element in the list
friends:
- name: "Steph"
age: 22
# Description and signature with specific formatting directives
description: > # Treats everything within as a single line
This is a single-line text despite the breaks, demonstrating YAML's flexibility in handling text blocks.
signature: | # Preserves the formatting
Gmail
Name
School
# Advanced structures with anchors and aliases
person_alias: &person
name: "Mike"
occupation: "Programmer"
friends_list:
- <<: *person
age: 22
relationship: "Colleague"
# Data type enforcement
age: !!float 23 # Interpreted as 23.0
gpa: !!str 3.5 # Interpreted as a string "3.5"
Example from My Blog, you see this _config.yml in my repo:
title: Rand Tech Blog
description: >-
I document things.
baseurl: ""
url: "https://github.com/kaden175ck/kaden175ck.github.io.git"
github_username: kaden175ck
# Build settings
theme: minima
plugins:
- jekyll-feed