DEV Community

Cover image for YAML: Yet Another Markup Language.
Joel Amos
Joel Amos

Posted on

YAML: Yet Another Markup Language.

Is it a failed language?

Introduction

YAML (YAML Ain’t Markup Language) is a human-friendly data serialization language used across various programming languages. Developed by Clark Evans, Ingy döt Net, and Oren Ben-Kiki in 2001, YAML is known for its readability and is commonly used for configuration files. Unlike Extensible Markup Language (XML) , YAML is designed for ease of human reading and writing, and it features a simpler syntax compared to Standard Generalized Markup Language (SGML).

YAML supports custom data types but fundamentally encodes scalars (strings, integers, and floats), lists, and associative arrays (maps, dictionaries, or hashes). It employs a colon-centered syntax for key-value pairs, an indentation style similar to Python’s, escape sequences like those in C, and whitespace handling inspired by HTML.

History

YAML emerged during an era dominated by markup languages such as HTML, _XML, and SGML . Its name is a recursive acronym meant to emphasize its focus on data serialization rather than markup.

Since its inception, YAML has seen several versions with added features and improvements, starting with the first beta, YAML 1.0 , released on January 29, 2004. This version aimed to create a more human-readable format compared to XML and JSON .

Subsequent versions were released later on and they include:
YAML 1.1 - 18 January 2005
This version introduced enhancements and clarifications to the original specification, focusing on improving the handling of data types and various data structures .
YAML 1.2.0 - 21 July 2009
Closely aligning to JSON, this release simplified the format and aimed to eliminate some obscurity present in the earlier releases.

Subsequent versions include:

  • YAML 1.1 - January 18, 2005: Introduced enhancements and clarifications to improve data type handling and data structures.
  • YAML 1.2.0 - July 21, 2009: Simplified the format and aligned closely with JSON to reduce obscurity.
  • YAML 1.2.1 - October 1, 2009: Made YAML a superset of JSON, ensuring compatibility with valid JSON documents.
  • YAML 1.2.2 - October 1, 2021: Focused on refining YAML while maintaining its core principles, enhancing usability, and expanding its capabilities.

Syntax

YAML has a simple syntax designed with readability in mind. The indentations in its syntax denote structures.
Its key features include:

Human-Readability:

  • Indentation represents nested structures, which visually declutters the script.

Data Types:

  • Scalars: Represent simple values such as strings, numbers, and boolean.
  • Sequences: Represent ordered lists (arrays). Commonly denoted by a dash ( - ) followed by a space.
  • Mappings: Represent key-value pairs (dictionaries). They are denoted by a colon (:) followed by a space.

Complex structures

  • Nested Sequences and Mappings: YAML supports nested structures, allowing complex data to be represented hierarchically.
  • References: It allows the use of anchors (&) and aliases (*) to reuse and reference data within the same document.

Formatting Rules:

  • Indentation: Relies on 2 to 4 spaces for nesting. NB: Tabs are not allowed.

  • Line Breaks: For multiple-lined strings, YAML provides various options like folded style ( < ) and literal style ( | ).

Comments:

  • Denoted by a #, and can be placed on their own line or at the end of a line.

File Extensions:

  • Typically uses .yaml or .yml extensions.

Interoperability:

  • As a superset of JSON, YAML supports JSON documents, making integration with JSON easier.

  • An example of a YAML syntax:

--- # Favorite movies
- Casablanca
- North by Northwest
- The Man Who Wasn't There

--- # Shopping list
[milk, pumpkin pie, eggs, juice]

--- # Indented Block
  name: John Smith
  age: 33
--- # Inline Block
{name: John Smith, age: 33}

--- #Strings
data: >
   Wrapped text
   will be folded
   into a single
   paragraph

   Blank lines denote
   paragraph breaks

---
example: >
        HTML goes into YAML without modification
message: |

        <blockquote style="font: italic 1em serif">
        <p>"Three is always greater than two,
           even for large values of two"</p>
        <p>--Author Unknown</p>
        </blockquote>
date: 2007-06-01
Enter fullscreen mode Exit fullscreen mode

Conclusions

Bearing in mind that YAML is still used widely in build and testing production-level environments, it proves to be an essential tool for managing configurations and data interchange. Its readability and flexibility make it a popular choice for defining automation scripts in Ansible , where it streamlines IT task automation. Similarly, YAML's role in Github Workflows facilitates the configuration of CI/CD pipelines, making testing and deployments more efficient. The continued evolution and integration of YAML in these critical areas underscore its ongoing relevance and effectiveness in simplifying complex workflows and configurations.
For me, if it works, the it is not a failure.

Top comments (0)