Monday, 24 February 2020

The Easy Understanding of YAML

YAML compared to JSON is like Python compared to JavaScript. YAML is smarter with tree of nodes instead of opening and closing blocks.

For a parent-child relation, it is read with the parent first in JSON:
  • Starting with { is an object
  • Starting with [ is an array
However, with the same parent-child relation, it is read with the child first in YAML:
  • A node followed by ':' (colon) means the parent is an object
  • A node starts with '- ' (dash and space) means the parent is a list
  • A node starts with '- ' (dash and space) and followed by ':' (colon) means it's starting key of an object and the parent is a list.
  • Indent below an object key to make child object key.
Examples of YAML
Example 1. Root is object
Foo: "value1"
Bar: "value2"

Example 2: Root is list
- "value1"
- "value2"

Example 3: Object (root) of objects
Foo:
  Child1: "value1"
  Child2: "value2"

Example 4: Object (root) of lists
Foo:
  Child1:
  - "value1"
  - "value2"
  Child2:
  - "value3"
  - "value4"

Example 4: List of items
List:
- "value1"
- "value2"

Example 5: List of objects
List:
- Foo: "value1"
  Bar: "value2"
- Foo: "value3"
  Bar: "value4"

No comments:

Post a Comment