Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Basic YAML Syntax Tutorial

Introduction

YAML (YAML Ain't Markup Language) is a human-readable data serialization standard that can be used in conjunction with all programming languages and is often used to write configuration files. In this tutorial, we will cover the basic syntax of YAML, providing detailed explanations and examples.

Basic Structure

YAML is used to represent data in a structured format. It uses indentation to denote nested structures, and it is sensitive to indentation. The basic structure of a YAML file includes key-value pairs separated by a colon and space:

key: value

Comments

Comments in YAML are denoted by the # character. Comments can be placed on their own line or at the end of a line of data.

# This is a comment
key: value  # This is an inline comment
                

Scalars

Scalars are the basic data types used in YAML, including strings, numbers, and booleans.

Strings

Strings can be written with or without quotes. If a string contains special characters or spans multiple lines, quotes are necessary.

single_quoted: 'This is a single-quoted string'
double_quoted: "This is a double-quoted string"
unquoted: This is an unquoted string
multiline: |
  This is a 
  multiline string
                

Numbers

Numbers in YAML can be written as integers or floating-point numbers.

integer: 25
float: 3.14
                

Booleans

Booleans are represented by the words true and false.

boolean_true: true
boolean_false: false
                

Lists

Lists in YAML are represented by a series of items preceded by a hyphen and space. Lists can be nested within other lists or dictionaries.

- item1
- item2
- item3
                

Here is an example of a nested list:

- item1
- item2
- 
  - nested_item1
  - nested_item2
                

Dictionaries

Dictionaries (also known as maps or hashes) in YAML are collections of key-value pairs. Keys are followed by a colon and space, and the values can be any valid YAML data type.

key1: value1
key2: value2
key3: value3
                

Dictionaries can also be nested:

outer_key:
  inner_key1: value1
  inner_key2: value2
                

Advanced Structures

Mixed Lists and Dictionaries

YAML allows mixing lists and dictionaries. Here is an example combining both:

- key1: value1
  key2: value2
- key3: value3
  key4: value4
                

Anchors and Aliases

YAML supports anchors (&) and aliases (*) to reuse repeated data. An anchor is defined using the & character, and an alias is referenced with the * character.

defaults: &defaults
  adapter: postgres
  host: localhost

development:
  database: dev_db
  <<: *defaults

test:
  database: test_db
  <<: *defaults
                

Conclusion

In this tutorial, we have covered the basic syntax of YAML, including its structure, comments, scalars, lists, dictionaries, and more advanced features like anchors and aliases. With this foundational knowledge, you can start using YAML to create configuration files and represent data in a human-readable format.