YAML syntax

YAML has features that come from Perl, C, XML, HTML, and other programming languages. YAML is also a superset of JSON, so JSON files are valid in YAML.

YAML uses Python-style indentation to indicate nesting. Tab characters are not allowed, so whitespaces are used instead. There are no usual format symbols, such as braces, square brackets, closing tags, or quotation marks. YAML files use a .yml or .yaml extension. 

The structure of a YAML file is a map or a list.

Maps allow you to associate key-value pairs. Each key must be unique, and the order doesn't matter. Think of a Python dictionary or a variable assignment in a Bash script.

A map in YAML needs to be resolved before it can be closed, and a new map is created. A new map can be created by either increasing the indentation level or by resolving the previous map and starting an adjacent map. 

A list includes values listed in a specific order and may contain any number of items needed. A list sequence starts with a dash (-) and a space, while indentation separates it from the parent. You can think of a sequence as a Python list or an array in Bash or Perl. A list can be embedded into a map. 

YAML also contains scalars, which are arbitrary data (encoded in Unicode) that can be used as values such as strings, integers, dates, numbers, or booleans.

When creating a YAML file, you’ll need to ensure that you follow these syntax rules and that your file is valid. A linter is an application that verifies the syntax of a file. The yamllint command can help to ensure you’ve created a valid YAML file before you hand it over to an application.     From REDHAT

Get started with YAML for beginners            


JSON is abbreviated as JavaScript Object Notation which is easy to understand and self-describing. It is available in standard text format and used for saving and transporting the data. JSON helps to transmit data in web applications where the data is sent from server to client and can be viewed on the web page. YAML is used for scripting configuration files and can be incorporated with added programming languages. It is popular for data serialization language, human-readable language, and adaptive. The difference and comparison of JSON and YAML are described in the article.   @   EDUCBA

Following example shows a YAML template with inline comments.          from   --->    AWS

AWSTemplateFormatVersion: "2010-09-09"

Description: A sample template


  MyEC2Instance: #An inline comment

    Type: "AWS::EC2::Instance"


      ImageId: "ami-0ff8a91507f77f867" #Another comment -- This is a Linux AMI

      InstanceType: t2.micro

      KeyName: testkey



          DeviceName: /dev/sdm


            VolumeType: io1

            Iops: 200

            DeleteOnTermination: false

            VolumeSize: 20

YAML cheatsheet links.




%YAML 1.1   # Reference card


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.

Scalar indicators:

  '''' : Surround in-line unescaped scalar ('' escaped ').

  '"'  : Surround in-line escaped scalar (see escape codes below).

  '|'  : 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').

Alias indicators:

  '&'  : Anchor property.

  '*'  : Alias indicator.

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").

Document indicators:

  '%'  : Directive indicator.

  '---': Document header.

  '...': Document terminator.

Misc indicators:

  ' #' : Throwaway comment indicator.

  '`@' : Both reserved for future use.

Special keys:

  '='  : Default "value" mapping key.

  '<<' : Merge keys from another mapping.

Core types: # Default automatic tags.

  '!!map' : { Hash table, dictionary, mapping }

  '!!seq' : { List, array, tuple, vector, sequence }

  '!!str' : Unicode string

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

  ? !!binary >


  : >-

      Base 64 binary value.

Escape codes:

 Numeric   : { "\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 }