YAML cheatsheet Syntax

yaml document contains multiple lines of text with following rules.

In high level, yaml documents are interpreted as Map or dictionaries

  • first line begins with ---
  • End of document with ...
  • each line of text contains key and value pairs like a map.
  • key and values are separated by colon (:) and space
  • use spaces instead of tab for indentation

simple key and value mapping

key: value

simple key and complex value mapping

key:< key2
    key3: value1 < value

Key accepts strings value can be one of following

  • Scalar types
  • sequence or collection type
  • nested collections

yaml indent/spaces

tab spaces (4 spaces) are not allowed and gives an error

There are two types syntaxes

  • Indented blocks
  • Inline blocks

Indented blocks syntax

Inline blocks uses indentation or spaces for each line new line is introduced to create a key and values

id: 1
name: Franc
salary: 5000

Inline blocks syntax

all the keys and value pairs are separated by comma and declared in braces, value is separted by colon and spaces

{id:1,name: Franc,salary: 5000}

yaml datatypes

The following data types are supported by YAML:

TypesDescription
StringStrings are enclosed with or without double or single quotes
numberIt can be integers or floating values
BooleanAccepts true or false
sequenceSupports array or list
SetSet stores the elements with unordered without duplicates
Mapmaps stores key with value as like hash map
binarysupport binary data of octal strings - images,files
timestampSupports date, datetime

Scalars types - number, boolean,strings

Scalars are of integers or floating values,strings, Boolean values Strings can be enclosed in single or double or unquoted

value:11
pivalue:3.14
isEnabled:false
strvalue1:'string value1'
strvalue2:"string value2"
strvalue3: string value3

Collection types

The following collections are supported

  • Sequence
  • Set
  • Nested Map or Dictionary

Sequence type

Sequence are like arrays or lists. Please see Sequence

Set

Set is an list of unordered elements. It does not preserve duplicates elements.

Set stores the elements with null values

There are two types of syntax representation

typed sets using specifying !!set tag and ? symbol makes the set

numbers :!! set
? one
? two
? three

flow style sets It is json representation of values using !!set

numbers: !!set { 'one', "two", three }

the equivalent of above two set examples as map

numbers:
  one: null
  two: null
  three: null

Dictionary or Nested Map

each line of document and key and value pairs Each value can be nested with other document using indentation. use 2 spaces is prerequisite, tabs are not supported

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