Skip to content

YAML

YAML stands for “YAML Ain’t Markup Language.” It’s a human-readable data format used to represent configuration and structured data.

YAML struct define by indentation Use spaces only not TAB'S Common style : 2 space per level

Tip

pip install --break-system-packages yamllint

Tip

pip install pyyaml

simple YAML

Dictionary (map / key-value)

simple.py
---
key: value

load and print

Simple python load yaml and print it using pprint

simple.py
from typing import Any
import yaml
import pathlib

def load_yaml(path: str) -> Any:
    """Load a single-document YAML file using safe_load."""
    with open(path, "r", encoding="utf-8") as f:
        return yaml.safe_load(f)

if __name__ == "__main__":
    file = pathlib.Path(__file__).parent.joinpath("simple.yaml").as_posix()
    data = load_yaml(file)
    print(data)
usage
1
2
3
4
python simple.py
#
<class 'dict'>
{'key': 'value'}

Comments

Anything on a line after # is ignored by YAML parsers.


List

Each - begins a new element in the sequence.

1
2
3
4
5
6
7
---
list:
  - apple
  - banana
  - orange

alternative: [grape, mango, pineapple]

Mapping

Unordered key-value pairs

1
2
3
4
5
6
7
---
worker:
  id: 22222
  name: me
  age: 32

alternative: {id:2222, age:30, name: dddd}

string

You don’t need quotes unless contain special character or start with {,[,#]

multiline

just folded
1
2
3
msg: >
  Hello
  world
multi line string
1
2
3
msg2: |
  Hello
  world

1
2
3
4
{
  'msg': 'Hello world\n', 
  'msg2': 'Hello\nworld'
}

null

1
2
3
value1: null
value2: ~
value3:
# simple.py result
{'value1': None, 'value2': None, 'value3': None}

types

  • string
  • boolean
  • integer
  • float
  • list
  • dict
  • null
int_val: 10
float_val: 3.14
bool_true: true
bool_false: false
string1: hello
string2: "hello"
list: [1, 2, 3, 4, 5]
dict:
  key1: value1
  key2: value2
null_val: null

linter

github source

pip install --break-system-packages yamllint

Configuration

1
2
3
4
5
6
7
extends: default

rules:
  # 80 chars should be enough, but don't fail if a line is longer
  line-length:
    max: 80
    level: warning
yamllint -c /path/myconfig file-to-lint.yaml

Schemas

YAML Language support uses JSON Schemas to understand the shape of a YAML file, including its value sets, defaults and descriptions. The schema support is shipped with JSON Schema Draft 7.

The association of a YAML file to a schema can be done either in the YAML file itself using a modeline or in the User or Workspace settings under the property yaml.schemas. in vscode settings

Associating a schema in the YAML file

# yaml-language-server: $schema=<urlToTheSchema>

VSCode

Extension

alt text

Settings

Schema

1
2
3
yaml.schemas: {
    "/home/user/custom_schema.json": "someFilePattern.yaml",
}