Indentation And Separation

YAML – Indentation and Separation

YAML (YAML Ain't Markup Language) is a human-oriented data serialization format designed to be more readable and writable than XML and JSON. Unlike XML and JSON, YAML is not a markup language; rather, it's a data format. It utilizes indentation and separation to represent data structures, eliminating the need for additional markup languages and making it more comprehensible and editable.

Indentation: YAML employs indentation to denote nesting, using spaces instead of tabs. Indentation must be consistent and composed of spaces only, without mixing spaces and tabs. For instance, the following example is a YAML document containing nested lists:

- name: Alice
  age: 25
  hobbies:
    - reading
    - hiking
- name: Bob
  age: 30
  hobbies:
    - cooking
    - swimming

In the above example, each list item starts with a hyphen followed by a space. Each line has the same level of indentation. Sub-items are indented two spaces more than their parent items to signify the nesting.

Separation: YAML uses separation to denote the relationship between keys and values. A colon (:) is used to separate keys and values, followed by a space. For instance, the following example is a YAML document containing key-value pairs:

name: Alice
age: 25
hobbies:
  - reading
  - hiking

In the above example, each key-value pair is separated by a colon, and there is a space between keys and values.

Multiline Text: Occasionally, a text value might be lengthy and span multiple lines. YAML provides three ways to represent multiline text: folded style, literal style, and folded block style.

Folded Style: When a multiline text value has no blank lines in between, you can use the folded style to represent it. The folded style merges the multiline text into a single line, removing trailing line breaks and spaces. In YAML, the folded style is represented using a vertical bar (|). For example:

description: |
  This is a long description
  that spans multiple lines.
  It will be folded into a
  single line.

Literal Style: The literal style preserves the multiline text as-is, including line breaks and spaces. In YAML, the literal style is represented using two vertical bars (||). For example:

message: ||
  This is a long message
  that spans multiple lines.
  It will be output as
  multiple lines.

Folded Block Style: The folded block style stores multiline text in a separate file and references that file in the YAML. Folded block style is represented using a less-than symbol (<). For example:

data: 
  < multi_line_data.txt

In the above example, the data will be read from the multi_line_data.txt file.

Last updated