Skip to content
-_- edited this page Sep 13, 2024 · 19 revisions

YAML is a text based format allowing programmers to store structured data in a hierarchy. YAML is designed to be human and machine readable with a minimum of overhead. The YAML specification can be found at yaml.org. There is also a reference card

Comments start with # and go till newline, comments must be separated from other tokens by whitespace. Whitespace isn't free, indentation must be spaces, not tabs. YAML will consider that lines prefixed with more spaces than the parent key are contained inside it. Moreover, all lines must be prefixed with the same amount of spaces to belong to the same map.

YAML has sequences and mappings as collection types, both can be represented in flow and block style.

An sequence of scalar strings in YAML looks like:

[ one, two, three ]   # flow style

# or block style

- one
- two
- three

A mapping consists of key/value pairs:

index: 4  # block style
name: nali

# or 

{ index: 4, name: nali }   # flow style

# or nested (equivalent of { level: { one: { two: fun } } }):

level:

  one:

    two: fun

Splitting text strings over multiple lines

- Without quotes:
   You can just
   split a long piece of text like this.
- With quotes:
    "[But be careful:
     if you \"need\" punctuation, put double quotes around it. You can ev\
     en split without spaces by using backslashes."
- Or single quotes:
    'This works
     but isn''t as flexible'
- If you want to keep those new line characters: | 
    Then do
    it this way with 
    a pipe (|) character. (This string has three \n characters)
- Or you can have just the one final new line: >
    This string has
    just one \n character, at the very end.
- Block indicators:
    Look up >-, >+, |- and |+ for fine tuning.

Basic YAML types

integer: 25
string: "25"
float: 25.0
boolean: true
null type: null

YAML supports three styles of escape notation:

  1. Entity Escapes

    a. space: " "

    b. colon: ":"

    c. ampersand: "&"

  2. Unicode Escapes

    a. space: "\u0020"

    b. single quote: "\u0027"

    c. double quote: "\u0022"

  3. Quoted Escapes

    a. double quote in single quote: 'Is "I always lie" a true statement?'

    b. nested double quote: " She said, "I quit" "

    c. nested single quote: ' He was speechless: '' '

I've been producing and consuming YAML lately. If you have objects serialized to text, you need some form of introspection to reconstruct them with a generic parser. The YAML parser does not know or care about the code in those other modules. It just assumes that the names in the input correspond to classes currently loaded by whomever called the parser. It relies on the language's introspection system to convert the strings in the YAML into calls to constructors.

Rules

https://yamllint.readthedocs.io/en/stable/rules.html

Tools

http://yaml-online-parser.appspot.com/

https://yamllint.com

https://github.com/mikestead/hx-yaml/blob/master/src/yaml/Parser.hx

Queries

+pyyaml "yaml lists" +dump

python yaml recipes activestate

yaml +python +ruby +dump

Scripts

Spellcheck against a dictionary

import yaml

# Function to check if a key contains any of the specified words
def contains_keywords(key, keywords):
    return any(keyword in key for keyword in keywords)

# Load the YAML file
with open('your_file.yaml', 'r') as file:
    data = yaml.safe_load(file)

# Keywords to check for
keywords = ["foo", "bar", "une"]

# Function to recursively check keys in the YAML data
def check_keys(data, keywords):
    if isinstance(data, dict):
        for key, value in data.items():
            if contains_keywords(key, keywords):
                print(f"Key '{key}' contains one of the keywords.")
            check_keys(value, keywords)
    elif isinstance(data, list):
        for item in data:
            check_keys(item, keywords)

# Check the keys in the loaded YAML data
check_keys(data, keywords)

References

https://metacpan.org/pod/Text::FrontMatter::YAML

https://pypi.org/project/python-frontmatter/

https://www.earthdata.nasa.gov/s3fs-public/imported/YAML%201.2%20Spec.pdf

https://builtin.com/software-engineering-perspectives/yaml-vs-json

https://www.snaplogic.com/blog/json-vs-yaml-whats-the-difference-and-which-one-is-right-for-your-enterprise

https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html

https://gist.github.com/oconnor663/9aeb4ed56394cb013a20

http://sweetohm.net/article/introduction-yaml.en.html

https://rtfm.co.ua/en/what-is-yaml-its-overview-basic-data-types-yaml-vs-json-and-pyyaml/

http://www.chrisrolle.com/en/blog/yaml-list

https://confit.readthedocs.io/en/latest/

https://itnext.io/what-is-yaml-its-overview-basic-data-types-yaml-vs-json-and-pyyaml-92059d1a24fa

https://www.redhat.com/sysadmin/yaml-tips

https://rollout.io/blog/yaml-tutorial-everything-you-need-get-started/

https://hackersandslackers.com/simplify-your-python-projects-configuration/

https://www.serendipidata.com/posts/safe-api-design-and-pyyaml

https://yaml.readthedocs.io/en/latest/api.html

http://docs.racket-lang.org/yaml/

https://news.ycombinator.com/item?id=17358103

https://www.infoq.com/articles/tosca-docker-orchestration/

https://garywoodfine.com/serverless-aws-lambda-dependency-injection/

Clone this wiki locally