YAML Tutorial: Simple Steps to Get Started

Mohmmad SaifMohmmad Saif
2 min read

YAML (YAML Ain't Markup Language) is a human-readable data serialization standard that can be used for configuration files and data exchange between languages with different data structures. Here’s a detailed explanation:

Key Features:

  1. Human-Readable: YAML is designed to be easy to read and write for humans. It uses indentation to represent the structure, similar to Python, which makes it visually clear.

  2. Data Serialization: YAML is used to represent data structures like objects, arrays, and primitive data types (strings, integers, etc.). It can serialize data into a format that can be easily parsed and used in different programming languages.

  3. Hierarchy and Indentation: YAML uses indentation (usually two spaces) to represent nested structures, such as lists and dictionaries. Indentation levels denote structure hierarchy.

  4. Flexible Data Types:

    • Scalars: Strings, numbers, booleans, and null values.

    • Sequences: Ordered lists.

    • Mappings: Key-value pairs, similar to dictionaries in Python.

Basic Syntax:

Scalars:

string: "Hello, World!"
integer: 123
float: 123.45
boolean: true
null_value: null

Sequences (Lists):

fruits:
  - Apple
  - Banana
  - Cherry

Mappings (Dictionaries):

person:
  name: John Doe
  age: 30
  address:
    street: 123 Main St
    city: Anytown
    state: CA

Mixed Types:

shopping_list:
  - item: Bread
    quantity: 2
  - item: Milk
    quantity: 1
    type: Whole
  - item: Apples
    quantity: 5
    type: Fuji

Advanced Features:

  1. Anchors and Aliases: Reuse data with anchors (&) and aliases (*).

     default_settings: &defaults
       enabled: true
       timeout: 30
    
     custom_settings:
       <<: *defaults
       timeout: 60
    
  2. Comments: Add comments using the # symbol.

     name: John Doe  # This is a comment
    
  3. Multi-line Strings: Use | for literal blocks and > for folded blocks.

     literal_block: |
       This is a literal block
       where newlines are preserved.
    
     folded_block: >
       This is a folded block
       where newlines are folded into spaces.
    

Use Cases:

  • Configuration Files: Widely used in applications for configuration, such as Docker Compose, Kubernetes, and Ansible.

  • Data Serialization: Used for data interchange between different programming environments.

  • Document Markup: Can be used to annotate data within documents.

Parsing and Generating YAML:

YAML can be easily parsed and generated in many programming languages using libraries like:

  • Python: PyYAML

  • JavaScript/Node.js: js-yaml

  • Ruby: Psych (part of the standard library)

Conclusion:

YAML is a powerful and flexible format for data serialization and configuration, emphasizing readability and ease of use. It is particularly suited for hierarchical data structures and is widely adopted across various domains for configuration management and data exchange.

0
Subscribe to my newsletter

Read articles from Mohmmad Saif directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohmmad Saif
Mohmmad Saif

👋Hello I am Mohd Saif, passionate technology enthusiast currently pursuing a bachelor of Computer application degree. 🎓Education: I am currently pursuing a bachelor of Computer application degree with the focus on coding at Bareilly University my education journey has equipped me with strong foundation in Computer science and I am eager to apply my knowledge to real word challenges. 💡Passion for technology: I have always been deeply passionate about technology and I am particular drawn to devops with AWS. 🚀Skills: 🔹Linux 🔹Shell scripting 🔹Python 🔹Ansible 🔹Docker 🔹Kubernetes 🔹Jenkins CI/CD 🔹Maven 🔹Git and GitHub ✨Future goals: My goal is to facilitate seamless collaboration between development and operations teams, ensuring faster releases and high-quality software. I am a proactive learner, constantly exploring new DevOps trends and practices