Skip to content

What is a YAML File?

  • Data

The abbreviation YAML is either “Yet Another Markup Language” or “YAML Ain’t Markup Language”, depending on the source. The language is used to serialize data, i.e. to convert structured data into a sequence. Whereby the first name is not really valid, because it is not a Markup Language.

The so-called markup languages, such as HTML or XML, use markers to structure textual information. In HTML, for example, there is a fixed set of markers written in angle brackets (<>). These include, for example, headings (<h1>, <h2>, <h3>, …) or paragraphs (<p>), which are used to build up the structure of a website.

How does the syntax of YAML work?

This file format uses functions from various programming languages, such as Perl, C, or HTML. At the same time, it is a so-called superset of JSON, which means that JSON files can be used in YAML without significant problems.

A YAML file starts with three hyphens (“—“) to indicate that the file is starting. It can have either the structure of a list or a map. In our example, we show a map with fixed key-value pairs, which are similar to Python dictionaries. Each key may occur only once and the order of the pairs is not specified:

---
name: Boris Miller
telephone: 0152 48912348
age: 28

What Data Types does the File Format support?

In a “Yet Another Markup Language” file the common data types can be stored and saved:

  • Boolean (True/False)
  • Integer (e.g. 5 or 2304)
  • Floating Point (decimal numbers, e.g. 2.48 or 1983.4335)
  • String
  • Null values

What do you use YAML for?

The creation of configuration files is one of the most common uses of “Yet Another Markup Language” files. For example, the links, user names, or IP addresses that software requires are stored centrally in these files. So that one does not have to search the entire code with a change, these are stored and collected in a file.

The “Yet Another Markup Language” is, as already mentioned, also used for data serialization in order to be able to send it over the Internet, for example. In addition to YAML, JSON or XML are also frequently used for this purpose.

Another important use case of these file types is the transfer of data between different programs via interfaces.

YAML and JSON in Comparison

These two file types are relatively similar in many respects and can therefore usually be used in the same applications. Both belong to the so-called text files, which store information in natural language and are thus readable by humans.

Since the newer versions, Yet Another Markup Language is also a superset of JSON files. This means that JSON files can be used in YAML files without any problems and do not cause errors.

The only, real difference is that JSON places a very high value on ease of use and is willing to degrade human readability to achieve this.

How to write and process YAML files in Python?

With the help of Python you can very easily write and open these files. There is even its own library for processing:

# Import modules
import yaml as yml

# open the example file
with open('details.yml') as f:

    data = yml.load(f, Loader=yml.FullLoader)
    print(data)

Out:
{"name": "Boris Miller", "telephone": "0152 48912348", "age": 28}

With the help of the library, our read sample file is directly converted into a Python Dictionary and is then available for further processing. Just as easily, Python dictionaries can be saved in “Yet Another Markup Language” files:

dict_file = [
{'cars' : ['A6', 'A180', 'M3', 'A4', 'X5']},
{'brands' : ['Audi', 'Mercedes', 'BMW', 'Audi', 'BMW']}
]

# Create new file and dump data into it
with open(data.yml', 'w') as file:  
    data = yml.dump(dict_file, file)

This is what you should take with you

  • YAML is the abbreviation for “Yet Another Markup Language” and is mainly used for data serialization.
  • It is very similar to JSON in its use and many other properties and is therefore also compatible.
  • Besides data serialization, YAML is also used for configuration files of software.

Other Articles on the Topic of YAML

  • You can find more information about Yet Another Markup Language on the official website.
close
Das Logo zeigt einen weißen Hintergrund den Namen "Data Basecamp" mit blauer Schrift. Im rechten unteren Eck wird eine Bergsilhouette in Blau gezeigt.

Don't miss new articles!

We do not send spam! Read everything in our Privacy Policy.

Cookie Consent with Real Cookie Banner