YAML Cheatsheet

YAML Cheatsheet

 

 

Grammar Rules

  • Case Sensitive
  • Use indentation to represent hierarchy
  • Tabs are not allowed when indenting, only spaces are allowed.
  • The number of indented spaces is not important, as long as the same level of elements is left-justified
  • # Represents the comment, from this character until the end of the line, will be ignored by the parser.

All YAML files can optionally begin with — and end with  This is part of the YAML format and indicates the start and end of a document.

Special Characters

'? ' : Key indicator.': ' : Value indicator.'- ' : Nested series entry indicator.', ' : Separate in-line branch entries.'[]' : Surround in-line series branch.'{}' : Surround in-line keyed branch.
'&' : Anchor property.'*' : Alias indicator.

Data Structure

  • Scalars: Individual, undividable values
  • Hashes: A collection of key-value pairs, also known as a mapping/object/dictionary
  • Sequences: A set of ordered values, also known as a array/list (list)

For more info learn YAML from here:YAML Tutorial

Scalars

The scalar quantity is the most basic and indivisible value.

Boolean_type_1: yesBoolean_type_2: Truenull_type_2: nullnull_type_3: ~number_type_1: 123number_type_2: 1.23string_type_2: 'hello'string_type_3: "hello"date: 2020-02-28	# date type#The time is in ISO8601 format.iso8601: 2001-12-14t21:59:43.10-05:00  # Enforcing stringsdate_as_string: !str 2020-02-28   # date type

Sequences

A set of ordered values, also known as a sequence/list/array (list)

# sequencearray:   - 12   - 3.34   - 'abc'   - true   - null

Nested sequence

- - Cat - Dog - Goldfish

Hashes

A collection of key-value pairs, also known as a mapping/hashes/dictionary

Hash

animal: pets

Nested hash

hash: subkey:   subsubkey1: 5   subsubkey2: 6 another:   somethingelse: 'Important!'

Composite structure

Objects and arrays can be combined to form a composite structure.

languages: - Ruby - Perl - Python websites: YAML: yaml.org  Ruby: ruby-lang.org  Python: python.org  Perl: use.perl.org

Multiple Documents

YAML uses three dashes — to separate directives from document content. This also serves to signal the start of a document if no directives are present. Three dots ( “…”) indicate the end of a document without starting a new one, for use in communication channels.

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

String in YAML

1. Block Notation: Newlines become spaces and extra newlines after the block are removed

---# Note: it has 1 new line after the stringcontent:    Arbitrary free text    over multiple lines stopping    after indentation changes......

Equivalent JSON

{ "content": "Arbitrary free text over multiple lines stopping after indentation changes..."}

2. Literal Block Scalar: a Literal Block Scalar | will include the newlines and any trailing spaces. but removes extra

newlines after the block.

---# After string we have 2 spaces and 2 new linescontent1: | Arbitrary free text over "multiple lines" stopping after indentation changes...  ...

Equivalent JSON

{ "content1": "Arbitrary free textnover "multiple lines" stoppingnafter indentation changes...  n"}

3. + indicator with Literal Block Scalar: keep extra newlines after block

---# After string we have 2 new linesplain: |+ This unquoted scalar spans many lines....

Equivalent JSON

{ "plain": "This unquoted scalarnspans many lines.nnn"}

4. – indicator with Literal Block Scalar: – means that the newline at the end of the string is removed.

---# After string we have 2 new linesplain: |- This unquoted scalar spans many lines....

Equivalent JSON

{ "plain": "This unquoted scalarnspans many lines."}

5. Folded Block Scalar(>):

will fold newlines to spaces and but removes extra newlines after the block.

---folded_newlines: > this is really a single line of text despite appearances...

Equivalent JSON

{ "fold_newlines": "this is really a single line of text despite appearancesn"}

YAML References

Anchors &and aliases *can be used for reference.

---values:- &ref Something to reuse- *ref	# Reused content

Merging Keys 

 &Used to create an anchor point ( defaults<<that merges into the current data and is *used to refer to the anchor point.

defaults: &defaults   adapter: postgres   host: localhostdevelopment:   database: myapp_development   <<: *defaultstest:   database: myapp_test   <<: *defaults

Ancor point for current data

- &us USA - UK - INDIA - CHINA - *us

Complex Mapping:

---? - key:  - value# Note: key and value can be multiple, complex structures

Mapping between Sequences

? - Detroit Tigers  - Chicago cubs:  - 2001-07-23? [ New York Yankees,    Atlanta Braves ]: [ 2001-07-02, 2001-08-12,    2001-08-14 ]

Corresponding JSON

{"["Detroit Tigers", "Chicago cubs"]": ["2001-07-23"],"["New York Yankees", "Atlanta Braves"]": [   "2001-07-02",   "2001-08-12",   "2001-08-14" ]}

Inheritance

parent: &defaults  a: 2  b: 3child:  <<: *defaults  b: 4

Reference link