YAML Introduction Tutorial
Category Programming Techniques
YAML is a recursive acronym for "YAML Ain't a Markup Language" (YAML is not a markup language). When this language was developed, the meaning of YAML was actually "Yet Another Markup Language" (still another markup language).
The syntax of YAML is similar to other high-level languages and can easily express lists, hashes, and scalars, among other data structures. It uses whitespace indentation and relies heavily on visual features, making it particularly suitable for expressing or editing data structures, various configuration files, debugging dumps, and file outlines (for example, many email title formats are very close to YAML).
The suffix for YAML configuration files is .yml
, such as: tutorialpro.yml.
Basic Syntax
Case-sensitive
Indentation is used to represent hierarchical relationships
Indentation must not use tabs, only spaces are allowed
The number of spaces for indentation does not matter, as long as elements at the same level are left-aligned
'#' indicates a comment
Data Types
YAML supports the following data types:
Object: A collection of key-value pairs, also known as a mapping, hashes, or dictionary
Array: A sequence of values in a specific order, also known as a sequence or list
Scalars: Single, indivisible values
YAML Object
Object key-value pairs are represented by the colon structure key: value, with a space following the colon.
You can also use key:{key1: value1, key2: value2, ...}.
Hierarchical relationships can also be represented using indentation;
key:
child-key: value
child-key2: value2
For more complex object formats, a question mark followed by a space can represent a complex key, combined with a colon and a space to represent a value:
?
- complexkey1
- complexkey2
:
- complexvalue1
- complexvalue2
This means that the object's attribute is an array [complexkey1, complexkey2], and the corresponding value is also an array [complexvalue1, complexvalue2].
YAML Array
Lines starting with -
represent an array:
- A
- B
- C
YAML supports multi-dimensional arrays and can be represented inline:
key: [value1, value2, ...]
If the data structure's sub-member is an array, you can indent with a space below that item.
-
- A
- B
- C
A relatively complex example:
companies:
-
id: 1
name: company1
price: 200W
-
id: 2
name: company2
price: 500W
This means that the companies attribute is an array, and each array element is composed of three attributes: id, name, and price.
Arrays can also be represented in a flow style:
companies: [{id: 1,name: company1,price: 200W},{id: 2,name: company2,price: 500W}]
Composite Structures
Arrays and objects can form composite structures, for example:
languages:
- Ruby
- Perl
- Python
websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org
Converted to JSON is:
{
languages: ['Ruby', 'Perl', 'Python'],
websites: {
YAML: 'yaml.org',
Ruby: 'ruby-lang.org',
Python: 'python.org',
Perl: 'use.perl.org'
}
}
Scalars
Scalars are the most basic, indivisible values, including:
Strings
Booleans
Integers
Floats
Null
Time
Date
Here is an example to quickly understand the basic use of scalars:
``` boolean: - TRUE #true,True both can be used - FALSE #false, False both can be used float: - 3.14 - 6.8523015e+5 #Scientific notation can be used int: - 123 - 0b1010_0111_0100_1010_1110 #Binary representation null: nodeName: 'node' parent: ~ #~ is used to represent null string: - haha - 'Hello world' #Double quotes or single quotes can be used to wrap special characters - newline