Introduction to YAML

Welcome to the magical world of YAML. By magical, we mean that it's magically simple. Let's unpack YAML and see if we can't sprinkle a bit of fairy dust on it, shall we?

What is YAML?

YAML, stands for "YAML Ain't Markup Language," is a human-readable data serialization language that is commonly used for configuration files, data exchange between languages with different data structures, and in applications where data is being stored or transmitted.

# This is how you define a simple YAML
name: John Doe
age: 35
city: Gotham

It's pretty easy on the eyes, right? The simplicity of YAML is what makes it so enchanting. And just like a good story, its beauty lies in its simplicity.

When and why to use YAML?


Now, you might be thinking, "Okay, cool, it's simple. But when and why should I use it?"

Excellent question, my astute apprentice! YAML is best used when the data is more complex, needing nested and complex structures, or when you need a human-readable data format. Its readability is a key feature - even non-programmers can easily understand a YAML document.

# An example of a more complex YAML
employees:
  - id: 1
    name: John Doe
    job: Developer
  - id: 2
    name: Jane Doe
    job: Designer

A mystical use-case of YAML is in DevOps for configuration files, like in Docker Compose or Kubernetes, because these files are often read, written, and updated by us humans.

Difference between YAML and other data serialization formats (JSON, XML)


"But what about JSON and XML? How does YAML compare to these older, wiser languages?"

Oh, eager one, you're on the right track. Let's see how our protagonist, YAML, holds up against these two serialization titans.

YAML vs JSON

JSON is also a human-readable format and is pretty straightforward. But if JSON were the elder wizard who speaks in careful, precise spells, YAML would be the playful sprite who conveys a lot with a little.

In fact, YAML is a superset of JSON. This means any valid JSON file is also a valid YAML file!

{
  "name": "John Doe",
  "age": 35,
  "city": "Gotham"
}
name: John Doe
age: 35
city: Gotham

Can you see how YAML appears cleaner, less cluttered with punctuation? That's the magic of YAML.

YAML vs XML

Now, XML is like the ancient dragon of data serialization. It's powerful, highly structured but it's not very human-friendly, especially when the data becomes complex.

<person>
  <name>John Doe</name>
  <age>35</age>
  <city>Gotham</city>
</person>
name: John Doe
age: 35
city: Gotham

Just by looking, which one would you prefer to use?


YAML's charm lies in its simplicity and human-friendliness. It's a tool every developer should have in their magical toolbox. In the next page of this tutorial we'll learn more about the YAML syntax.

Previous Next

Written © 2024 Written Developer Tutorials and Posts.

𝕏