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:

Types Description
String Strings are enclosed with or without double or single quotes
number It can be integers or floating values
Boolean Accepts true or false
sequence Supports array or list
Set Set stores the elements with unordered without duplicates
Map maps stores key with value as like hash map
binary support binary data of octal strings - images,files
timestamp Supports 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

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 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

  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

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