YAML CheatSheet

If you're a developer, mastering the YAML syntax is an essential skill you can't afford to overlook. Our comprehensive YAML cheat sheet provides an organized and easy-to-use reference guide that covers everything from basic syntax rules to advanced data structures. This professional-grade resource is designed for developers of all levels, providing clear examples and explanations to help you quickly get up to speed.

With our YAML cheat sheet, you'll have access to a wealth of knowledge at your fingertips. Whether you're looking to create complex data structures or simply need a refresher on basic YAML concepts, our cheat sheet has got you covered. It's the perfect resource for streamlining your development process and ensuring that your code is clean, efficient, and error-free.

So what are you waiting for? Download our YAML cheat sheet today and take your development skills to the next level! With its clear and concise format, it will quickly become an indispensable tool in your coding toolkit.


Table of Content




# Getting started YAML


What is YAML?

YAML is a data serialization language that is often used for writing configuration files.

YAML stands for yet another markup language or YAML aint markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.

YAML is a popular programming language because it is human-readable and easy to understand. It can also be used in conjunction with other programming languages.


  • YAML does not allow the use of tabs
  • Must be space between the element parts
  • YAML is CASE sensitive
  • End your YAML file with the .yaml or .yml extension
  • YAML is a superset of JSON
  • Ansible playbooks are YAML files

YAML Advantages

  • In YAML, there is no extra delimiter is used. So it is the lightweight than XML and JSON.
  • YAML, not using delimiter also makes the reading light and simple. (I am not sure if you agree with this point. Because many geeks find it easier to read the data in content delimiter or tags separate those.)
  • YAML makes the data understanding easy. So it is useful in case of configuration.
  • YAML has a wide variety of uses and popular for. It can be used for configuration, then from transferring data to storing intermediate data.
  • YAML is the superset of JSON. You heard it right. Whatever valid JSON code you have, it will be parsed by YAML compiler. So choosing YAML over JSON for your project you have one advantage. You can parse both JSON and YAML code with the single parser (YAML parser).

YAML Disadvantages

  • There are so many applications that are already build using XML or JSON, so it hard for a developer to replace this with YAML.
    For instance, I am developing a plugin for the existing project which uses XML. I would like to use XML in the plugin. So it will be easy for me to merge plugin with an existing project.
  • Talking about the popularity, XML has much more matured ecosystem than YAML.
  • JSON [JavaScript Object Notation] exists since early 2000s and it is highly adopted by many as compare to YAML. So it is easier to find the support for JSON over YAML.
  • There are extra precautions while writing YAML code. Even if you mismatch single space while indentation, your code can stop working.
  • There are many ways to represent data in YAML and make data hierarchy. So it is complex for processing. So JSON and XML have better performance than YAML.

Scalar types


n1: 2            # integer          
n2: 1.23444        # float      

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

b: true         # boolean type 

d: 2016-04-07    # date type

Json Preview


{
  "n1": 2,
  "n2": 1.23444,
  "s1": "abcdf",
  "s2": "abcdf",
  "s3": "abcdf",
  "b": true,
  "d": "2016-04-07"
}

Variables


some_thing: &VAR_NAME foobar

other_thing: *VAR_NAME

Json Preview


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

Comments

# A single line comment example

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

Multiline strings


description: |
  hello
  bestforstudy.com

Json Preview


{"description": "hello\nbestforstudy.com\n"}

Inheritance


parent: &defaults
  a: 2
  b: 3

child:
  <<: *defaults
  b: 4

Json Preview


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

Reference


values: &ref
  - Will be
  - reused below
  
other_values:
  i_am_ref: *ref

Json Preview


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

Folded strings


description: >
  hello
  world

Json Preview


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

Two Documents

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

# Collections in YAML


Sequence


- Jackson
- Jack
- Jess

Json Preview


[
  "Jackson",
  "Jack",
  "Jess"
]

Mapping


hr:  65       # Home runs
avg: 0.278    # Batting average
rbi: 147      # Runs Batted In

Json Preview


{
  "hr": 65,
  "avg": 0.278,
  "rbi": 147
}

Mapping to Sequences


attributes:
  - b1
  - c2
methods: [getter, setter, void]

Json Preview


{
  "attributes": ["b1", "c2"],
  "methods": ["getter", "setter", "void"]
}

Sequence of Mappings


children:
  - name: Jimmy Smith
    age: 15
  - name: Jimmy Smith
    age: 15
  -
    name: Sammy Sosa
    age: 12

Json Preview


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

Sequence of Sequences


my_sequences:
  - [1, 2, 3]
  - [4, 5, 6]
  -  
    - 7
    - 8
    - 9
    - 0 

Json Preview


{
  "my_sequences": [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9, 0]
  ]
}

Mapping of Mappings


Jack: {
    hr: 65, 
    avg: 0.278
}

Jess: {
    hr: 63,
    avg: 0.288
}

Json Preview


{
  "jack": {
    "hr": 65,
    "avg": 0.278
  },
  "Jess": {
    "hr": 63,
    "avg": 0.288
  }
}

Nested Collections


Jack:
  id: 1
  name: Franc
  salary: 25000
  hobby:
    - a
    - b
  location: {
      country: "A", 
      city: "A-A"
  }

Json Preview


{
  "Jack": {
    "id": 1,
    "name": "Franc",
    "salary": 25000,
    "hobby": [
      "a",
      "b"
    ],
    "location": {
      "country": "A",
      "city": "A-A"
    }
  }
}

Unordered Sets


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

Json Preview


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

Ordered Mappings


ordered: !!omap
- Jack: 65
- Jess: 63
- Messi: 58

Json Preview


{
  "ordered": [
    {
      "Jack": 65
    },
    {
      "Jess": 63
    },
    {
      "Messi": 58
    }
  ]
}

# YAML Reference


Terms

  • Sequences aka arrays or lists
  • Scalars aka strings or numbers
  • Mappings aka hashes or dictionaries

Document indicators

% Directive indicator
--- Document header
... Document terminator

Collection indicators

? Key indicator
: Value indicator
- Nested series entry indicator
, Separate in-line branch entries
[] Surround in-line series branch
{} Surround in-line keyed branch

Alias indicators

& Anchor property
* Alias indicator

Special keys

= Default "value" mapping key
<< Merge keys from another mapping

Scalar indicators

'' Surround in-line unescaped scalar
" Surround in-line escaped scalar
| Block scalar indicator
> Folded scalar indicator
- Strip chomp modifier (|- or >-)
+ Keep chomp modifier (|+ or >+)
1-9 Explicit indentation modifier (|1 or >2).
Modifiers can be combined (|2-, >+1)

Tag Property (usually unspecified)

none Unspecified tag (automatically resolved by application)
! Non-specific tag (by default, !!map/!!seq/!!str)
!foo Primary (by convention, means a local !foo tag)
!!foo Secondary (by convention, means tag:yaml.org,2002:foo)
!h!foo Requires %TAG !h! <prefix> (and then means <prefix>foo)
!<foo> Verbatim tag (always means foo)

Misc indicators

# Throwaway comment indicator
`@ Both reserved for future use

Core types (default automatic tags)

!!map {Hash table, dictionary, mapping}
!!seq {List, array, tuple, vector, sequence}
!!str Unicode string

Escape Codes

Numberic

  • \x12 (8-bit)
  • \u1234 (16-bit)
  • \U00102030 (32-bit)

Protective

  • \\ (\)
  • \" (")
  • \ ( )
  • \<TAB> (TAB)

C

  • \0 (NUL)
  • \a (BEL)
  • \b (BS)
  • \f (FF)
  • \n (LF)
  • \r (CR)
  • \t (TAB)
  • \v (VTAB)

Additional

  • \e (ESC)
  • \_ (NBSP)
  • \N (NEL)
  • \L (LS)
  • \P (PS)

More types

!!set {cherries, plums, apples}
!!omap [one: 1, two: 2]

Language Independent Scalar Types

{~, null} Null (no value).
[1234, 0x4D2, 02333] [Decimal int, Hexadecimal int, Octal int]
[1_230.15, 12.3015e+02] [Fixed float, Exponential float]
[.inf, -.Inf, .NAN] [Infinity (float), Negative, Not a number]
{Y, true, Yes, ON} Boolean true
{n, FALSE, No, off} Boolean false


Best Suggest