Skip to content

yaml language tutorial ​

Introduction ​

YAML is the recursive abbreviation of "YAML Ain't a Markup Language".When the language was developed, YAML actually stood for: "Yet Another 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 for indentation and a lot of appearance-dependent features, specifically Suitable for expressing or editing data structures, various configuration files, dumping debugging content, and file outlines (For example: many email header formats are very close to YAML).

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

yaml is often used as a configuration file, similar to json

basic grammar ​

  • Case sensitive

  • Use indentation to indicate hierarchical relationships

  • Tabs are not allowed for indentation, 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 comment

data type ​

YAML supports the following data types:

  • Object: A collection of key-value pairs, also known as mapping/hashes/dictionary
  • Array: A set of values arranged in order, also called sequence/list
  • Scalar (scalars): a single, irreducible value

YAML object ​

Several representations of objects:

1. The object key-value pair is represented by the colon structure key: value, and a space must be added after the colon.

yaml
key: value

2. You can also use key:{key1: value1, key2: value2, ...}.

yaml
key:{child-key1:value1,child-key2:value2}

3. You can also use indentation to indicate hierarchical relationships;

yaml
key: 
    child-key1: value1
    child-key2: value2

Same as 2, only the representation is different.

4. For more complex object formats, you can use a question mark plus a space to represent a complex key, and a colon plus a space to represent a value:

yaml
?  
    - complexkey1
    - complexkey2
:
    - complexvalue1
    - complexvalue2

This means that the attribute of the object is an array [complexkey1,complexkey2], and the corresponding value is also an array [complexvalue1,complexvalue2]

YAML array ​

Lines beginning with - represent an array:

yaml
- A
- B
- C

YAML supports multidimensional arrays, which can be represented inline:

yaml
key: [value1, value2, ...]

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

yaml
-
 - A
 - B
 - C

A relatively complex example:

yaml
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 using flow:

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

Composite structures (combination of objects and arrays) ​

Arrays and objects can form composite structures, for example:

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

Converted to js as:

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

scalar quantity ​

Scalars are the most basic, irreducible values, including:

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

Use an example to quickly understand the basic use of scalars:

yaml
boolean: # 布尔值
    - TRUE  #true,True都可以
    - FALSE  #false,False都可以
float: # 浮点数
    - 3.14
    - 6.8523015e+5  #可以使用科学计数法
int: # 整数
    - 123
    - 0b1010_0111_0100_1010_1110    #二进制表示
null: # Null
    nodeName: 'node'
    parent: ~  #使用~表示null
string: # 字符串
    - 哈哈 # 字符串默认不使用引号表示
    - 'Hello world'  #可以使用双引号或者单引号包裹特殊字符
    - newline
      newline2    #字符串可以拆成多行,每一行会被转化成一个空格
date:
    - 2018-02-17    #日期必须使用ISO 8601格式,即yyyy-MM-dd
datetime: 
    -  2018-02-17T15:02:31+08:00    #时间使用ISO 8601格式,时间和日期之间使用T连接,最后使用+代表时区

Multi-line strings can use | to preserve newlines, or > to collapse newlines.

yaml
this: |
  Foo
  Bar
that: >
  Foo
  Bar

The converted js code is as follows.

json
{ this: 'Foo\nBar\n', that: 'Foo Bar\n' }

+ means to keep the newline at the end of the text block, and - means to delete the newline at the end of the string.

yaml
s1: |
  Foo
 
s2: |+
  Foo
 
 
s3: |-
  Foo

The code converted to json is as follows

json
{ s1: 'Foo\n', s2: 'Foo\n\n\n', s3: 'Foo' }

HTML tags can be inserted into strings.

yaml
message: |
  <p style="color: red">
    段落
  </p>

Convert data format ​

Two exclamation points are allowed to cast the data type.

yaml
e: !!str 123
f: !!str true

The result of converting js is the same:

json
{ e: '123', f: 'true' }

anchor reference ​

& anchors and < span class="marked">* aliases can be used to reference:

yaml
defaults: &defaults # 添加锚点
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  <<: *defaults # <<表示合并,*引用锚点

test:
  database: myapp_test
  <<: *defaults # <<表示合并,*引用锚点

Equivalent to:

yaml
defaults:
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  adapter:  postgres
  host:     localhost

test:
  database: myapp_test
  adapter:  postgres
  host:     localhost

& is used to establish anchor points (defaults), << means merging into the current data, and ***** is used to reference anchor points.

Here's another example:

yaml
- &showell Steve 
- Clark 
- Brian 
- Oren 
- *showell

The converted js code is as follows:

yaml
[ 'Steve', 'Clark', 'Brian', 'Oren', 'Steve' ]

Reference: