DEV Community

Ludivine A
Ludivine A

Posted on • Updated on

How to write a YAML file

YAML means "Yet Another Markup Language" in version 1.0, and became "YAML Ain't Markup Language" in 1.1 . Created in 2001 by Clark Evans, it was meant to be more elaborate than CSV and as simply written, (or even more), than XML.

The key to master YAML is to be very accurate when it comes to indentation. Here the indentation acts as a structure for the file.

Just like JSON, YAML works with a Key-value pair system :

Strings

Putting your strings between quotes is not necessary, unless you want to do multiple-lines.

last-name: Doe
first-name: 'John'
Enter fullscreen mode Exit fullscreen mode

Multiple-lines takes two forms, one which doesn't keep the format :

bar: >
Once upon a time, there was
a beautiful princess
named Snow White.
Enter fullscreen mode Exit fullscreen mode

is interpreted as :

bar: Once upon a time, there was a beautiful princess named Snow White.
Enter fullscreen mode Exit fullscreen mode

And one which keeps the format as is :

bar: |
Once upon a time, there was
a beautiful princess
named Snow White
Enter fullscreen mode Exit fullscreen mode

will be interpreted as :

bar : Once upon a time, there was
a beautiful princess
named Snow White
Enter fullscreen mode Exit fullscreen mode

Numbers

integer: 506
negative-integer: -374
float: 103.98
Enter fullscreen mode Exit fullscreen mode

Nulls

foo: null
bar: ~
Enter fullscreen mode Exit fullscreen mode

Booleans

day: true
night: false
Enter fullscreen mode Exit fullscreen mode

Comments

# List of users
name: 'John Doe' # A user
Enter fullscreen mode Exit fullscreen mode

Objects

planets: { Mars: 'terrestrial planet', Jupiter: 'gas giant' }
Enter fullscreen mode Exit fullscreen mode

Arrays

cities: ["Reykjavik", "Kuala Lumpur"]
# or
cities:
- "Paris"
- "New York"
Enter fullscreen mode Exit fullscreen mode

you can make multiple-lines arrays :

groceries:
  - fruits:
      apples: 3
      bananas: 1
  - other:
      - milk: semi-skimmed
Enter fullscreen mode Exit fullscreen mode

I hope my article helped you understand YAML a little bit more !
Thank you for reading 👋 !


Originally posted on my blog. Check out my instagram account to learn more about web development.

Discussion (0)