Learning YAML – YAML Ain’t Markup Language | Hacker Noon

Shveta Vasisht Hacker Noon profile picture

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

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

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

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

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.

For further reading please refer to YAML Ain’t Markup Language (YAML™) Version 1.2 3rd Edition

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.

read original article here