YAML (YAML Ain't Markup Language) is a human-readable data serialization format commonly used for configuration files and data exchange between languages with different data structures. Here are the basic concepts of YAML that you should understand:
1. Basic Structure
-
Key-Value Pairs: YAML uses key-value pairs to represent data. The key is followed by a colon and a space, then the value.
name: John Doe age: 30
2. Indentation
-
Indentation: YAML uses indentation (spaces, not tabs) to denote structure. Indentation levels indicate hierarchy.
person: name: John Doe age: 30
3. Lists
-
Lists: You can create lists using a hyphen followed by a space. Lists can be nested.
fruits: - Apple - Banana - Cherry -
Nested Lists:
groceries: - fruits: - Apple - Banana - vegetables: - Carrot - Broccoli
4. Dictionaries (Maps)
-
Dictionaries: A dictionary is a collection of key-value pairs. You can nest dictionaries within dictionaries.
person: name: John Doe age: 30 address: street: 123 Main St city: Anytown
5. Comments
-
Comments: You can add comments in YAML using the
#symbol. Comments are ignored by the parser.# This is a comment name: John Doe # This is an inline comment
6. Data Types
-
Strings: Strings can be unquoted or quoted (single or double quotes).
unquoted_string: Hello single_quoted_string: 'Hello' double_quoted_string: "Hello" -
Numbers: Numbers can be integers or floating-point.
integer: 42 float: 3.14 -
Booleans: Boolean values can be represented as
trueorfalse.is_student: true
7. Anchors and Aliases
-
Anchors: You can create anchors to reuse parts of your YAML file.
defaults: &defaults name: John Doe age: 30 person1: <<: *defaults city: New York person2: <<: *defaults city: Los Angeles
8. Multi-line Strings
-
Block Style: Use
|for multi-line strings where line breaks are preserved.description: | This is a multi-line string. It preserves line breaks. -
Folded Style: Use
>for multi-line strings where line breaks are converted to spaces.description: > This is a folded string. It will be a single line.
Summary
These basic concepts of YAML provide a foundation for understanding how to structure data in a human-readable format. By mastering these concepts, you can effectively use YAML for configuration files, data serialization, and more.
If you have any more questions or need further clarification on any specific concept, feel free to ask!
