YAML

This is a quick reference cheat sheet for understanding and writing YAML format configuration files.

#Getting started

#Introduction

YAML is a data serialisation language designed to be directly writable and readable by humans

#Scalar types

n1: 1            # integer          
n2: 1.234        # float      

s1: 'abc'        # string        
s2: "abc"        # string           
s3: abc          # string           

b: false         # boolean type 

d: 2015-04-05    # date type

#Equavalent JSON

{
  "n1": 1,
  "n2": 1.234,
  "s1": "abc",
  "s2": "abc",
  "s3": "abc",
  "b": false,
  "d": "2015-04-05"
}

Use spaces to indent. There must be space between the element parts.

#Variables

some_thing: &VAR_NAME foobar
other_thing: *VAR_NAME

#Equavalent JSON

{
  "some_thing": "foobar",
  "other_thing": "foobar"
}

#Sequence

object:
  attributes:
    - a1
    - a2
    - a3
  methods: [getter, setter]

#Equavalent JSON

{
  "object": {
    "attributes": ["a1", "a2", "a3"],
    "methods": ["getter", "setter"]
 }
}

#Sequence of sequences

my_sequences:
  - [1, 2, 3]
  - [4, 5, 6]

#Equavalent JSON

{
  "my_sequences": [
    [1, 2, 3], 
    [4, 5, 6]
  ]
}

#Comments

# A single line comment example

# block level comment example
# comment line 1
# comment line 2
# comment line 3

#Multiline strings

description: |
  hello
  world

#Equavalent JSON

{"description": "hello\nworld\n"}

#Folded text

description: >
  hello
  world

#Equavalent JSON

{"description": "hello world\n"}

#Hashes

jack:
  id: 1
  name: Franc
  salary: 5000
  hobby:
    - a
    - b
  loc: {country: "A", city: "A-A"}

#Equavalent JSON

{
  "jack": {
    "id": 1,
    "name": "Franc",
    "salary": 5000,
    "hobby": ["a", "b"],
    "loc": {
        "country": "A", "city": "A-A"
    }
  }
}

#Nested dictionaries

Employee:
  id: 1
  name: "Franc"
  department:
    name: "Sales"
    depid: "11"

#Equavalent JSON

{
  "Employee": {
    "id": 1,
    "name": "Franc",
    "department": {
      "name": "Sales",
      "depid": "11"
    }
  }
}

#Sequence of dictionaries

children:
  - name: Jimmy Smith
    age: 15
  - name: Jenny Smith
    age: 12

#Equavalent JSON

{
  "children": [
    {"name": "Jimmy Smith", "age": 15},
    {"name": "Jenny Smith", "age": 12}
  ]
}

#Set

set1: !!set
  ? one
  ? two
set2: !!set {'one', "two"}

#Equavalent JSON

{
  "set1": {"one": null, "two": null},
  "set2": {"one": null, "two": null}
}

#Inheritance

parent: &defaults
  a: 2
  b: 3

child:
  <<: *defaults
  b: 4

#Equavalent JSON

{
  "parent": {"a": 2, "b": 3},
  "child": {"a": 2, "b": 4}
}

#Reference

values: &ref
  - These values
  - will be reused below
  
other_values:
  i_am_ref: *ref

#Equavalent JSON

{
  "values": [
    "These values",
    "will be reused below"
  ],
  "other_values": {
    "i_am_ref": [
      "These values",
      "will be reused below"
    ]
  }
}

#Documents

---
document: this is doc 1
---
document: this is doc 2
...