> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# YAML Part 1

> Introductory guide to YAML covering its syntax, comparison with XML and JSON, lists and mappings, indentation rules, common constructs and best practices for configuration and manifests

In this lesson we introduce YAML and show how it compares with other common data-serialization formats. If you’ve worked with XML or JSON before, many concepts will feel familiar; if not, follow the examples and try them locally to build intuition.

YAML (YAML Ain't Markup Language) is a human-friendly data-serialization format commonly used for configuration files, CI/CD pipelines, Kubernetes manifests, and other structured data.

Quick side-by-side example
Below is the same small dataset represented in XML, JSON, and YAML so you can quickly compare readability and conciseness.

XML

```xml theme={null}
<Servers>
  <Server>
    <name>Server1</name>
    <owner>John</owner>
    <created>12232012</created>
    <status>active</status>
  </Server>
</Servers>
```

JSON

```json theme={null}
{
  "Servers": [
    {
      "name": "Server1",
      "owner": "John",
      "created": "12232012",
      "status": "active"
    }
  ]
}
```

YAML

```yaml theme={null}
Servers:
  - name: Server1
    owner: John
    created: 12232012
    status: active
```

Take a moment to compare the three: YAML is intentionally concise and optimized for human readability.

Comparison at a glance

| Format | Readability | Use Cases | Notes |
| - | - | - | - |
| XML | Verbose, highly structured | Document-centric data, legacy systems | Tag-heavy; good for schemas |
| JSON | Compact and widely used | Web APIs, configs, data interchange | Strict syntax (quotes, commas) |
| YAML | Very human-friendly, minimal syntax | Configuration files, manifests | Sensitive to indentation and whitespace |

Basic key-value pairs
A YAML key-value pair uses a colon to separate the key and value. For readability and to avoid parsing issues, put a space after the colon.

```yaml theme={null}
fruit: apple
vegetable: carrot
liquid: water
meat: chicken
```

Lists (arrays)
Represent lists with a dash (`-`) before each item. Leave a space after the dash.

```yaml theme={null}
fruits:
  - apple
  - banana
  - orange

vegetables:
  - carrot
  - potato
  - spinach
```

Mappings / dictionaries
Group related keys under a single parent key by indenting the nested properties.

```yaml theme={null}
Banana:
  Calories: 105
  Fat: 0.4 g
  Carbs: 27 g
```

Indentation and spacing
Indentation defines structure in YAML. Use a consistent indentation style (2 spaces per level is common) and never mix tabs and spaces.

Incorrect indentation can change parse results or produce errors. In the example below, `Fat` and `Carbs` are indented incorrectly so they appear to be children of `Calories` — which is invalid because `Calories` already has a scalar value.

Incorrect (invalid)

```yaml theme={null}
Banana:
  Calories: 105
    Fat: 0.4 g
    Carbs: 27 g
```

This typically triggers errors like "mapping values are not allowed here" because a node cannot be both a scalar and a mapping at the same time. Keep the same indentation level for sibling properties.

<Callout icon="lightbulb" color="#1CB2FE">
  Use a consistent indentation style (2 spaces per level is common). Avoid mixing tabs and spaces. Consistent indentation prevents unexpected nesting and parser errors.
</Callout>

<Callout icon="warning" color="#FF6B6B">
  Always put a space after the colon (`Key: Value`) and after the dash in list items (`- item`). Omitting these spaces can cause subtle parsing errors in some YAML parsers.
</Callout>

Nested structures (lists of mappings, mappings with lists)
YAML can express arbitrarily nested structures. The example below shows a list of fruits where each item is a mapping containing nutritional properties.

```yaml theme={null}
Fruits:
  - Banana:
      Calories: 105
      Fat: 0.4 g
      Carbs: 27 g

  - Grape:
      Calories: 62
      Fat: 0.3 g
      Carbs: 16 g
```

Notes about this structure:

* `Fruits` is a key whose value is a list.
* Each list element (`- Banana:` and `- Grape:`) is a mapping with its own nested keys (`Calories`, `Fat`, `Carbs`).

Common YAML constructs

| Construct | Syntax example |
| - | - |
| Key-value | `key: value` |
| List | `items:\n  - item1\n  - item2` |
| Nested mapping | `parent:\n  child: value` |
| List of mappings | `- name: foo\n  age: 10` |

Best practices and tips

* Use consistent indentation (2 spaces per level recommended).
* Always use a space after colons and dashes.
* Prefer explicit quoting for values that could be interpreted (e.g., `yes`, `no`, `on`, `off`, numeric strings).
* Validate YAML with a linter or parser before applying it to production systems (CI checks are helpful).
* When authoring large files, split logical sections into separate files and reference them where supported (e.g., Kubernetes manifests with kustomize or Helm values).

Summary

* YAML is a concise, human-readable format for structured data and configuration.
* Use `key: value` pairs, `-` for lists, and indentation to create nested structures.
* Be consistent with spacing and indentation to avoid parser errors.

Links and references

* [YAML Official Website](https://yaml.org/)
* [YAML Tutorial (learnyaml.org)](https://learnxinyminutes.com/docs/yaml/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/crash-course-kubernetes-for-absolute-beginners/module/c648e3a3-f425-456e-af2d-4d5526626094/lesson/bf6bb56d-d5af-409e-879d-13f7138530b0" />

  <Card title="Practice Lab" icon="flask-conical" cta="Learn more" href="https://learn.kodekloud.com/user/courses/crash-course-kubernetes-for-absolute-beginners/module/c648e3a3-f425-456e-af2d-4d5526626094/lesson/84b06439-ee10-452c-b63a-397fa79e310f" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.