foreplay

YAML is a recursive acronym for "YAML Ain't a Markup Language" (YAML is not a markup language). When this language was developed, YAML actually meant: "Yet Another Markup Language" (still a markup language).

The syntax of YAML is similar to other high-level languages, and it can simply express data forms such as lists, hash tables, and scalars. It uses whitespace indentation and a lot of appearance-dependent features, especially suitable for expressing or editing data structures, various configuration files, dumping debugging content, file outlines (for example: many email header formats are very close to YAML).

The configuration file suffix of YAML is  .yml , such as: eg.yml  .

basic grammar

  1. Case Sensitive
  2. Use indentation to indicate hierarchical relationships
  3. Tabs are not allowed for indentation, only spaces are allowed
  4. The number of spaces indented is not important, as long as the elements of the same level are left-aligned
  5. '#' means comment

type of data

YAML supports the following data types:

  1. Object: a collection of key-value pairs, also known as mapping/hashes/dictionary
  2. Array: A set of values ​​arranged in order, also known as a sequence (sequence) / list (list)
  3. scalars: single, indivisible values

object type

A set of key-value pairs of an object, represented by a colon structure

apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx

Yaml also allows another way to write all key-value pairs as an inline object

hash: { 
name: Steve,
age: 18
}

array type

A line starting with - means to form an array:

containers:
- name: nginx
- containerPort: 80

YAML supports multidimensional arrays, which can be represented using inline:

containers: [name,containers]

A child member of a data structure is an array, you can indent one space below the item.

-
- A
- B
- C

example:

companies:
-
id: 1
name: company1
price: 200W
-
id: 2
name: company2
price: 500W

It 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 fashion:

companies: [{id: 1,name: company1,price: 200W},{id: 2,name: company2,price: 500W}]

Composite structure

Objects and arrays can be combined to form composite structures

languages:
- Ruby
- Perl
- Python
websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org

Converted to json as:

{ 
languages: [ 'Ruby', 'Perl', 'Python'],
websites: {
YAML: 'yaml.org',
Ruby: 'ruby-lang.org',
Python: 'python.org',
Perl: 'use.perl.org'
}
}

 scalar

Scalars are the most basic, indivisible values, including:

  • string
  • Boolean value
  • integer
  • floating point number
  • Null
  • time
  • date