DEV Community

Cover image for Introduction to YAML

Introduction to YAML

Paula Santamaría on October 19, 2019

The first time I came across YAML was around a year ago when I use it to write OpenAPI definitions to document a RESTful API using Swagger API Docu...
Collapse
 
orenovadia profile image
orenovadia

Thanks for all the tips and tricks. I like using YAML for configurations.

PS: YAML has some default casting one should be aware of:

In [4]: yaml.load('yes') # 'Yes' and 'No' become boolean
Out[4]: True

In [5]: yaml.load('1_000_000')
Out[5]: 1000000

Enter fullscreen mode Exit fullscreen mode
Collapse
 
paulasantamaria profile image
Paula Santamaría • Edited

Thank you! According to the YAML 1.2 specification document 'yes' and 'no' are no longer interpreted as boolean.

We have removed unique implicit typing rules and have updated these rules to align them with JSON's productions. In this version of YAML, boolean values may be serialized as “true” or “false”;

You can use !!bool to parse them, though.

Collapse
 
_darrenburns profile image
Darren Burns

Thanks for this! I just got started with GitHub Actions a couple of days ago, and was making a lot of assumptions on what the YAML was representing -- the translations to JSON you've done here are really helpful :)

Collapse
 
paulasantamaria profile image
Paula Santamaría

Thanks Darren, I'm glad I could help! 🙂

Collapse
 
jacksoft profile image
Jacksoft CS

I used YAML file to configure Cluster group in the Pipeline during my internship this past Summer. It was a bit challenging since this was the first time using it but definitely easy to work with. Thanks for sharing this great article.

Collapse
 
paulasantamaria profile image
Paula Santamaría

Nice! Was it your first time working with it? And did its readability made it easier to pickup?

Collapse
 
karamfd profile image
Karam • Edited

I use YAML for my default config files with tools such as ESLint and Stylelint. I find the syntax a lot more intuitive than JSON and I am less likely to make mistakes with it.

Thank you for making this tutorial, Paula.

Collapse
 
paulasantamaria profile image
Paula Santamaría

Nice! Do you mind sharing a bit more about how you use it? Like which language and do you use a parser?
I'm really curious because I love the syntax, but I don't like the idea of including extra dependencies just for that.

Collapse
 
karamfd profile image
Karam

I currently use yaml only when a node js package offers built-in support for it. I stick with json, otherwise. I don't know much about parsers as I've only used babel before and it doesn't support yaml, as far as I know.

Collapse
 
rnrnshn profile image
Olimpio

I use YAML so often on Jekyll.. and I didn't know I could write multiline strings just by add this |. Amazing. Thanks.

Collapse
 
paulasantamaria profile image
Paula Santamaría

Something I forgot to include about strings is that you can also write multiline strings that you don't want to be interpreted as multiline. For example:

single-line-string: > 
    This
    should
    be
    one
    line

And this is how it'll look like in JSON:

{
    "single-line-string": "This should be one line\n"
}

When using the > character, instead of |, each new line will be interpreted as an empty space.

Collapse
 
paulasantamaria profile image
Paula Santamaría

I'm glad! Thanks for reading :D

Collapse
 
chr0m1ng profile image
Gabriel Santos

Hi, Paula. Great article!
I have one question, in JSON we can easily create a list of objects without “naming” them, like this:
[{“a”: “b”}, {“x”: “y”}]
How we do that in YAML? For example, a list of Authors (I don’t wanna have [{“authors” : {authoObj1}}, {“authors” : {authoObj2}}])

Collapse
 
paulasantamaria profile image
Paula Santamaría • Edited

Great question!
You can achieve that by entering the hyphen first and then the properties in a new line, like so:

- 
    name: George
    last-name: Orwell
- 
    name: Stephen
    last-name: King

Which will translate to JSON as:

[
    {
        "name": "George",
        "last-name": "Orwell"
    },
    {
        "name": "Stephen",
        "last-name": "King"
    }
]

Also here's a nice online tool I've been using to try the YAML syntax and see its JSON counterpart.

Collapse
 
abir1997 profile image
Abir

I used YAML for my uni assignment where we had to build a ci/cd pipeline using Travis CI for a spring boot application. Travis uses a YAML file for configuration and I found it very easy and intuitive to work with.

Collapse
 
paulasantamaria profile image
Paula Santamaría • Edited

Nice! Every CI pipeline config I've seen so far uses YAML. I believe we'll be seeing more of it in the near future.

Collapse
 
m1g profile image
Miguel

Thanks for this helpful article. I'm using it in K8s to setup the values files and sort of understood the basics of YAML - but this was legit lightbulb. I don't know why I didn't consider that it can be translated to JSON! Thanks again

Collapse
 
antorrg profile image
Antonio R. Rodriguez Gramajo

Thanks for all the tips and tricks!!
I discovered Yaml accidentally, I am learning star and I met him, and also with the link to your article and this beautiful community, this is for me a day of pleasant discoveries.

Collapse
 
tuwang profile image
TuWang

I ran into YAML when working with AWS CloudFormation. It beat me up like a drum 😒

Collapse
 
paulasantamaria profile image
Paula Santamaría

Believe me, I know the feeling!😂
One of the things that made me change my mind after reading about it for a bit was that the whole concept behind YAML made me remember how I felt after going from XML to JSON, which was basically "wow, so much less code, I can actually read this!".

Collapse
 
nexus5ooo profile image
nexus5ooo

I use yaml for Puppet and Ansible

Collapse
 
paulasantamaria profile image
Paula Santamaría

I didn't know any of those so I had to do a quick search. Sounds interesting!

Collapse
 
cemuka profile image
Cem Ugur Karacam

I use it since Jekyll themes use it as configuration data(front matter). As you described, at first glance it seemed weird but once you spend some time, you get used to it.

Collapse
 
paulasantamaria profile image
Paula Santamaría

Yes! Once I found out I could use comments and felt confortable enough to structure the info however I liked I realized how easy it was.

Collapse
 
thecreator232 profile image
AbhishekUnnikrishnan

I've been using YAML for the last 2 years and I've completely ignored learning anything about it. This was a good beginning.

Thanks

Collapse
 
paulasantamaria profile image
Paula Santamaría

Same here 😂. Its focus in readability makes it easy to pickup without actually learning it, but I always felt a bit uncomfortable with it until I wrote this, I also hated having to google specific stuff like "multi-line strings in yaml".

Collapse
 
mrprashantshinde profile image
Prashant Shinde

Super

Collapse
 
paulasantamaria profile image
Paula Santamaría

Thanks!

Collapse
 
antlio profile image
Anthony Lio

Yay thanks for that post, I've been using it slightly but happy to have learned something on it!

Collapse
 
paulasantamaria profile image
Paula Santamaría

Great! I'm glad I could help :)

Collapse
 
mahmoudai profile image
Mahmoud Ahmed

I start using YAML in docker and from that time I find it so useful and simple it makes me feel I read an article, not a data file.
Thanks Paula for this amazing article.

Collapse
 
paulasantamaria profile image
Paula Santamaría

Thank yo Mahmoud!
I've been meaning to get into docker, maybe after learning about YAML it'll be easier.

Collapse
 
devhammed profile image
Hammed Oyedele

Docker Compose to be precise ✌️✌️✌️

Collapse
 
nielwebdev profile image
niel-webdev

Nice Article! But now JSON also support reusability with "key" : $ref

Collapse
 
krutikkhatri profile image
krutikkhatri

Hey yamlonline.com/
checkout this editor for YAML to JSON
try it!!

Collapse
 
emptyother profile image
emptyother

So YAML does for JSON what Markdown did for HTML.

But I'm no fan of YAMLs "no tabs" rule or the "as many spaces as you want" rule. Seems a odd choice backed by a flimsy excuse.

Collapse
 
paulasantamaria profile image
Paula Santamaría

I found those a bit odd too at first. However, the YAML specification addresses the no tab rule:

Note that most modern editors may be configured so that pressing the tab key results in the insertion of an appropriate number of spaces.

And I found that to be true for VSCode at least.

And about the number of spaces, I just handle that myself and keep it consistent. Haven't had an issue with that, to be honest.

Collapse
 
krutikkhatri profile image
krutikkhatri

Hey checkout this yamlonline.com/
It's a nice tools for YAML to JSON try it!!