What is: YAML – its overview, basic data types, YAML vs JSON, and PyYAML
Tags: yaml, json, python
Published: 2019-03-15 10:17:24

---
title: What is: YAML – its overview, basic data types, YAML vs JSON, and PyYAML
published: true
tags: YAML,JSON,Python
canonical_url: https://rtfm.co.ua/en/what-is-yaml-its-overview-basic-data-types-yaml-vs-json-and-pyyaml/
---

![](https://rtfm.co.ua/wp-content/uploads/2016/01/What-is-it-e1452250201867.jpg)YAML – is one of the most popular formats of the…

Well, actually, they don’t know the format of what…

Originally it was the «_Yet Another Markup Language_», later it became «_YAML Ain’t Markup Language_»:

> Originally YAML was said to mean _Yet Another Markup Language_,<sup id="cite_ref-YAML_spec_2001_08_01_12-0"><a href="https://en.wikipedia.org/wiki/YAML#cite_note-YAML_spec_2001_08_01-12">[12]</a></sup> referencing its purpose as a [markup language](https://en.wikipedia.org/wiki/Markup_language "Markup language")with the [yet another](https://en.wikipedia.org/wiki/Yet_another "Yet another") construct, but it was then repurposed as _YAML Ain’t Markup Language_, a [recursive acronym](https://en.wikipedia.org/wiki/Recursive_acronym "Recursive acronym"), to distinguish its purpose as data-oriented, rather than document markup.

In the Russian Wikipedia the “friendly” word even is taken in quotes – and I absolutely agree with that.

In fact – YAML is just another one data serialization type, the successor of the JSON format, but with some additional abilities.

In the recent survey in the [Ukrainian DevOps Community](https://ukrops.club/) “_YAML vs JSON”_ – YAML took about 90% votes.

As for me JSON still the most convenient, but YAML used in many places so have to use it.

In this post will take a closer look at YAML’s data types and a quick comparison with JSON.

### YAML main principles

- always use UTF-8 to avoid possible issues
- never use TAB for indentation

### YAML syntax validation

To check YAML syntax in Linux the [`yamllint`](https://github.com/adrienverge/yamllint) can be used.

Install it:

```
$ sudo pacman -S yamllint
```

And check file:

```
$ yamllint monitoring.yml
monitoring.yml
1:1       warning  missing document start "---"  (document-start)
20:34     error    trailing spaces  (trailing-spaces)
22:32     error    trailing spaces  (trailing-spaces)
23:37     error    trailing spaces  (trailing-spaces)
33:7      error    wrong indentation: expected 8 but found 6  (indentation)
35:9      error    wrong indentation: expected 10 but found 8  (indentation)
36:11     error    wrong indentation: expected 12 but found 10  (indentation)
```

Although this file used by Ansible without any problems – there are still some issues in its formatting.

#### JSON validation

And for example – JSON documents validation from Linux console using Python’s `json` module:

```
$ python -m json.tool < json-example.json
{
  "key1": "value1",
}
```

### `vim` plugin

There is also the [`vim-yaml`](https://vimawesome.com/plugin/vim-yaml-all-too-well) plugin for `vim`.

Add to your `.vimrc`:

```
...
" https://vimawesome.com/plugin/vim-yaml-all-too-well
Plug 'avakhov/vim-yaml'
" add yaml stuffs
au! BufNewFile,BufReadPost *.{yaml,yml} set filetype=yaml foldmethod=indent
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab
...
```

Reload config and install it:

```
:source %
:PlugInstall
```

### PyYAML

To work with YAML from Python there is the [PyYAML](https://yaml.readthedocs.io/en/latest/example.html) library.

Some examples below.

### YAML formatting

#### Comments in YAML

One of the few advantages of YAM is an ability to add comments in its files.

Comments formatting is usual – using the #.

Comment can be added in any place.

Examples:

```
---
# I'm comment
- name: somestring
  value1: "# I'm not a comment!"
  value: anotherstring  # another comment
```

#### Indentations

The main headache on the YAML is the indentations.

In this, in a whole file, the number of spaces (spaces – never TABs!) must be the same.

I.e. if in one place two spaces are used – then the whole file must use two spaces.

Even more – the agreement is to use two spaces, although can be any – just has to be the same everywhere.

For example:

```
---
parent_key:
    key1: "value1"
    key2: "value2"
    key3: "%value3"
```

Will be valid form, but the next example:

```
---
parent_key1:
    key1: "value1"
    key2: "value2"
    key3: "%value3"
parent_key2:
  key1: "value1"
  key2: "value2"
  key3: "%value3"
```

Will not.

While in Python which is ofter is scolded because of the spaces dependency such formatting can be used, although will be standard’s violation:

```
#!/usr/bin/env python
def a():
    print("A")
    
def b():
  print("B")
  
a()
b()
```

Results:

```
$ python spaces.py
A
B
```

#### Single-line YAML

Besides the standard view and spaces indentation – you can use JSON-like formatting like:

```
---
parent_key: {key1: "value1", key2: "value2"}
```

#### Literal Block Scalar

YAML supports the ability to add multiline literal block scalars and has three types of it: the common one, using the “`|`” and the “`>`“.

The common format looks like:

```
---
string: This
    is
    some text
    without newlines
```

Result in Python console:

```
>>> yaml.load(open('yaml-example.yml'))
{'string': 'This is some text without newlines'}
```

Using the  | (_[Literal style)](http://www.yaml.org/spec/1.2/spec.html#id2795688)_ – will save all newlines and closing spaces:

```
---
string: |
    This
    is
    some text
    with newlines
```

Result is:

```
>>> yaml.load(open('yaml-example.yml'))
{'string': 'This\nis\nsome text\nwith newlines\n'}
```

And using the > (_[Folded style](http://www.yaml.org/spec/1.2/spec.html#id2796251)_):

```
---
string: >
    This
    is
    some text
    without newlines
```

Will return whole text in one line + closing newline symbol:

```
>>> yaml.load(open('yaml-example.yml'))
{'string': 'This is some text without newlines\n'}
```

But still, you have to adhere to the same spaces formatting.

Also, check the great answer on the StackOverflow [here>>>](https://stackoverflow.com/a/21699210/2720802):

> There are 5 6 NINE (or 63*, depending how you count) different ways to write multi-line strings in YAML.

### YAML basic data formats

YAML uses three main data formats::

- scalars: the simplest in a _key:value_ view
- list/sequence: data ordered by indexes
- dictionary/mapping: similar to scalars but can contain nested data including other data types

* * *

#### Scalars

Basic data type – scalars, just a key:value as programming variables:

```
---
key1: "value1"
key2: "value2"
```

Using quotes for values recommended to avoid possible issues with special characters:

```
cat example.yml
---
key1: "value1"
key2: "value2"
key3: %value3
```

Validate it:

```
$ yamllint example.yml
example.yml
4:7       error    syntax error: found character '%' that cannot start any token
```

Still, you can skip quotes for boolean true/false values and for integer types.

##### Scalars – YAML vs JSON

For example – scalar in YAML:

```
---
key: "value"
```

And JSON:

```
{
    "key": "value"
}
```

##### Python

The YAML-scalars in Python example:

```
>>> import yaml
>>> yaml.load("""
... key: "value"
... """)
{'key': 'value'}
```

Or from the file:

```
>>> import yaml
>>> yaml.load(open('yaml-example.yml'))
{'key': 'value'}
```

* * *

#### Lists in YAML

Lists, sequences, collections – represents a collection of an ordered data where each element can be accessed by its index.

For example:

```
# SIMPLE LIST
- element1
- element2
```

##### Nested lists in YAML

Similarly to the examples above – lists can include nested lists:

```
# SIMPLE LIST
- element1
- element2
# nested list
-
  - element1
```

Or can be a named list:

```
---
itemname:
  - valuename
```

In doing so lists can also contain scalars or dictionaries:

```
---
itemname:
  - valuename
  - scalar: "value"
  - dict: {item1: "value1", item2: "value2"}
```

##### Lists – YAML vs JSON

List in YAML:

```
---
- item1
- item2
- item3
```

List in JSON:

```
[
    "item1",
    "item2",
    "item3"
]
```

Nested list in YAML:

```
---
- item1
- item2
- item3
-
  - nested1
```

Nested list in JSON:

```
[
    "item1",
    "item2",
    "item3",
    [
        "nested1"
    ]
]
```

##### Python and YAML-lists

Here are all similar to scalar’s example:

```
>>> yaml.load(open('yaml-example.yml'))
['item1', 'item2', 'item3', ['nested1']]
>>> for i in yaml.load(open('yaml-example.yml')):
...   print(i)
...
item1
item2
item3
['nested1']
```

* * *

#### Dictionaries

Dictionaries, also called mappings is similar to scalars type and contains a key:value data but unlike scalars which are basic type – dictionary can include nested elements, for example, a list:

```
---
key1: "value1"
key2:
  - value2
  - value3
```

Or another nested dictionary:

```
---
key1: "value1"
key2:
  - value2
  - value3
    
key3:
  key4: "value4"
  key5: "value5"
  key6:
    key7: "value7"
```

##### Dictionary – JSON vs YAML

Dictionary in YAML:

```
---
key1: "value1"
key2:
  - value2
  - value3
```

Dictionary in JSON:

```
{
    "key1": "value1",
    "key2": [
        "value2",
        "value3"
    ]
}
```

##### Python

```
>>> yaml.load(open('yaml-example.yml'))
{'key1': 'value1', 'key2': ['value2', 'value3']}
>>> type(yaml.load(open('yaml-example.yml')))
<class 'dict'>
```

And all usual for Python’s dictionaries operations are supported:

```
>>> dict = yaml.load(open('yaml-example.yml'))
>>> type(dict)
<class 'dict'>
>>> dict.update({'key3':'value3'})
>>> print(dict)
{'key1': 'value1', 'key2': ['value2', 'value3'], 'key3': 'value3'}
```

* * *

In general – that’s all.

Check also those pages for more details:

- [Complete idiot’s introduction to yaml](https://github.com/Animosity/CraftIRC/wiki/Complete-idiot%27s-introduction-to-yaml)
- [YAML vs JSON](http://sangsoonam.github.io/2017/03/13/yaml-vs-json.html)
- [YAML Syntax](https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html)
- [YAML Syntax](https://learn.getgrav.org/15/advanced/yaml)
- [Learn X in Y minutes](https://learnxinyminutes.com/docs/yaml/)


### Similar posts
 
- <small>05/13/2015</small> [Django: создание проекта "домашняя бухгалтерия"](https://rtfm.co.ua/django-sozdanie-proekta-domashnyaya-buxgalteriya/)<small> (0)</small>  
- <small>02/05/2015</small> [Django Book: URLconf и "слабые связи"](https://rtfm.co.ua/django-book-urlconf-i-slabye-svyazi/)<small> (0)</small>   
- <small>05/31/2015</small> [Python: интеграция Pylint и PyCharm](https://rtfm.co.ua/python-integraciya-pylint-i-pycharm/)<small> (0)</small>