Skip to content

YAML

homepage-banner

Introduction

YAML is a lightweight data serialization language. The syntax of YAML is similar to Python’s syntax, using indentation and line breaks to represent structural hierarchy, without the need for tags or attributes to mark data. Compared to formats like XML and JSON, the YAML format is more compact and concise. YAML is very suitable for describing complex data structures and is widely used in configuration files, task and process definitions, data exchange, etc.

Basic Concept

  • YAML is not a Markup Language
  • YAML is used to store a variety of information
  • It allows us to define key-value pairs like variables, lists, and objects
  • YAML is very similar to JSON (Javascript Object Notation)
  • Its primary focus is on readability and user friendliness
  • YAML is designed to be clean and easily readable
  • YAML files can be defined with two different extensions: .yml or .yaml

YAML – Key Value Pairs

  • YAML documents consist of key-value pairs.
  • The key and value are separated by a colon.
  • There must be a space after the colon to differentiate the value.
  • YAML supports various data types such as:
  • Integer
  • Floating-point numbers
  • Strings
  • Boolean
  • Dates (in ISO 8601 format)
  • Null values
  • Important Note
Name: Dave
Age: 29
Gpa: 4.2
Occupation: Engineer
State: 'New Jersey'
AboutMe: "I am a software engineer"
Male: true
DateOfBith: 1990-09-15T15:53:00
PoliceCases: null

YAML – List / Array

  • YAML lists are indented with an opening dash.
  • The dash indicates that it’s an element of an array.
  • All members of a list begin at the same indentation level, starting with a “-” (a dash and a space).
  • Block sequences indicate each entry with a dash and space.
  • Flow sequences are written as comma-separated lists within square brackets.
Block Sequence
Persons:
- Dave
- John
- Mike
- Sam
Flow Sequence
Persons: [Dave, John, Mike, Sam]

YAML Dictionary / Map

  • YAML dictionaries are sets of properties grouped under an item.
  • YAML dictionaries contain key-value pairs.
Dave:
Age: 25
Occupation: Engineer
State: New Jersey
gpa: 4.5
male: true

YAML Lists containing Dictionaries

YAML-Lists-containing-Dictionaries.png

YAML Lists containing Dictionaries containing Lists

YAML-Lists-containingDictionaries-containing-Lists.png

YAML Pipe

  • The pipe notation, also known as a literal block.
  • All new lines, indentations, and extra spaces are preserved exactly as they are.
Dave:
  Age: 25
  Address: |
    201 A St.
    New York 10000
    000-111-222

YAML Greater than Sign

  • The “greater than” sign notation is also known as a folded block.
  • This renders the text as a single line.
  • All newline characters are replaced with a single space.
  • Blank lines are converted into newline characters.

YAML-Greater-than-Sign.png

YAML Comments

  • We can have comments in YAML with # sign.

Reference

  • https://hasnainm.hashnode.dev/intro-to-yaml
  • https://www.codeproject.com/Articles/1214409/Learn-YAML-in-five-minutes
Feedback