Learning YAML - YAML Ain't Markup Language

Written by shvetavasisht | Published 2021/04/13
Tech Story Tags: software-development | programming | coding | tutorial | yaml | markup | programming-languages | learn-programming

TLDR YAML Ain’t Markup Language is a data serialization language. It is used to transfer data between applications and services which have different data structures. It uses Unicode printable characters, some of which provide structural information and the rest contain the data. It has clear formatting which makes it human-readable, easy to use, and is easily implemented. It minimizes the number of structural characters and allows the data to be represented in a meaningful way. For example, indentation is used for structure, key-value pairs are separated by colons, and dashes are used to create bullet lists.via the TL;DR App

YAML is a data serialization language. It is used to transfer data between applications and services which have different data structures.
Originally YAML meant Yet Another Markup Language. However, it was later repurposed for YAML Ain’t Markup Language to emphasize its data-oriented features.
YAML was designed to be useful and friendly while working with data. It uses Unicode printable characters, some of which provide structural information and the rest contain the data.
It has clear formatting which makes it human-readable, easy to use, and is easily implemented.
YAML minimizes the number of structural characters and allows the data to be represented in a meaningful way. For example, indentation is used for structure, key-value pairs are separated by colons, and dashes are used to create bullet lists. Hence the format appears clearer and can be easily understood.
Let us take an example and compare it with JSON and XML:-
YAML
Employees:
  -  id: 4
     employeename: Ryan                  
     employeetitle: Marketing Manager 

JSON
{
"Employees": [
  {
    "id":  4,
    "employeename":  "Ryan",
    "employeetitle":  "Marketing Manager"
  }
]
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<Employees>
   <id>4</id>
   <employeename>Ryan</employeename>
   <employeetitle>Marketing Manager</employeetitle>
</Employees>
You can see that YAML does not use any special characters and is easier to read. It is the cleanest format of all three.
YAML matches the native data structures of agile methodology. The data structures can all be adequately represented with three basic primitives:
  1. mappings (hashes/dictionaries)
  2. sequences (arrays/lists)
  3. scalars strings/numbers)
YAML leverages these to form a complete language for serializing any native data structure.
Most programming languages can use YAML for data serialization, including agile languages such as Perl, Python, PHP, Ruby, and JavaScript. Common use cases include writing configuration files, log files, and data sharing.

Basic Syntax

  • YAML is case-sensitive.
  • YAML file extensions are .yaml , .yml
  • Data structures are defined using line separations and whitespace indentations. Tabs are not allowed in YAML.
  • Numerals
    Unquoted numerals are considered as integers or floating-point values.
    Integers, octal, or hexadecimal values can be specified as:
    id: 4
    octalexample: 012345    
    hexaexample: 0x12d4 
    Floating-point values can be fixed and exponential.
    weight: 55.5
    exp: 12.3015e+05
  • Strings
String data types are usually not included in quotes but you can use double or single quotes.
 firstemployeename: “Ryan”
 secondemployeename: ‘Ryan’
 thirdemployeename: Ryan
Multiline strings- You can specify multiline strings in two ways:-
Pipe character (|) is used to preserve line breaks.
multilineString: |
          this is a multiline string
          this is the second line
          this is the third line
The fold character or greater-than sign ( >) folds the text such that it all appears in one line.
 multilineString: >
         this is a single line string
         but is written in this format
         for clarity
  • Comments
Comments start with a hash sign (#).
Multiline comments are not supported. For multiline comments, you have to put # at the start of each line.
# Write your comment here
  • Key-value pairs
    The basic building blocks of yaml files are key-value pairs. These are represented in the form key: value. The colon must be followed by a space.
    Employees:
             id: 4
             employeename: Ryan                  
             employeetitle: Marketing Manage
    This can also be specified inline by using curly braces {} and separating each entry by a comma.
    Employees: {id: 4, employeename: Ryan, employeetitle: Marketing Manager}
    • Objects
    You can group key-value pairs in objects. Leading spaces have to be same for each attribute in an object
    This is a valid format:-
    Employees:
            id: 4
            employeename: Ryan                  
            employeetitle: Marketing Manager
    In the example given below, you can see that the attribute employeename is not indented correctly. Below example is an invalid YAML format.
     Employees:
            id: 4
          employeename: Ryan                  
            employeetitle: Marketing Manager
    • Lists
    List members are specified with a leading hyphen (-). Each entry is on a new line.
    List of simple data types
     Employees:
            -Ryan
            -Jack
    Lists can also be written in square brackets. The entries are comma-separated.
    Employees: [Ryan, Jack]
    List of objects -You can also specify lists of objects in yaml.
    Employees:
           - id: 4
             employeename: Ryan                  
             employeetitle: Marketing Manager
           - id: 5
             employeename: Jack                  
             employeetitle: Product Manager
    • Boolean
    Booleans can have values True/true/TRUE and False/false/FALSE
    Employees:
           - id: 4
             employeename: Ryan                  
             employeetitle: Marketing Manager
             onleave: True
           - id: 5
             employeename: Jack                  
             employeetitle: Product Manager
             onleave: False
    • YAML supports multiple documents in a stream.
    Three hyphens(—) are used to specify the beginning of a new YAML document. You can optionally use three periods (…) to mark the end of a document.
    ---
         Employees:
           - id: 4
             employeename: Ryan                  
             employeetitle: Marketing Manager 
    ...
    ---
         Departments:
          - id: 1
            departmentname: Marketing 
    ...

    Conclusion

    YAML, a superset of JSON, is a powerful yet user-friendly language. It is popular for its minimalism and simplicity, and useful for programming needs such as configuration files, Internet messaging, object persistence, and data sharing.

    Written by shvetavasisht | Tech learner.
    Published by HackerNoon on 2021/04/13