YAML

YAML ain't markup language. But it is. YAML is a 'data serialisation standard', or basically a way to convert your data into a text format that can be converted back while maintaining integrity. It's technically a superset of JSON, which means you can use JSON syntax within it. Check out the spec.

Why should you learn yet another thing like this? Why shouldn't we just use JSON. Well, like when we went from csv to XML, then XML to JSON, YAML is just a little easier on the eyes. Yaml is more strict on parsing too, so no duplicate keys like in JSON.

Lets start building something.

Basic as you can get

Three hyphens at the top of the text file indicates that the content is in YAML format.

---

Comments

Ooooh look! You can comment!

---
# My first yaml

Object

---
face:  9    # Out of 2 I'd give her one
body: 4     # I'm kinda outta shape
activity: 3 # Divvent dee much

Array, list, sequence

All the same to me. This would generate an array.

 - Tom
 - Dick
 - Harry

Array of nested objects

course:
  -
    section:
      -
        title: Section One

Multiline strings

  • | - preserves line breaks
  • > - single carriage return at the end
  • >- - no carriage returns

Stuff

Listing out all this stuff would be really boring. Instead take a look at the examples.

Yaml will know if your content is a string or numeric based on the data type. You can wrap it around quotes, single or double, if you want.

Yaml is also like Python in that it is based on spacing, so you can have a nested array based on spacing.

---
- - 1
  - 2
  - - 3
  - - 5 

or

---
- a: test
- b:
  - 1
  - 2 

Tools