ConfigurationYAML

YAML Ain’t Markup Language

YAML is a language for serializing data that can be directly writable and readable by humans. It has gained immense popularity in recent years. AWS CloudFormation template, OpenAPI, Swagger, Kubernetes, etc. use YAML to create human-readable configurations.

  • Tabs are not allowed in YAML
  • There must be space between the element parts
  • YAML is CASE sensitive
  • End your YAML file with the .yaml or .yml extension
  • YAML is a superset of JSON

Let’s look at how JSON can be represented in YAML

Sample JSON
{
  "basics": {
    "name": "Neeraj Kushwaha",
    "email": "kushneeraj@yahoo.com",
    "profession": "Software Engineer",
    "profiles": [
      {
        "network": "LinkedIn",
        "url": "https://www.linkedin.com/in/neerajkushwaha"
      }
    ]
  }
}
YAML Equivalent
basics:
  name: Neeraj Kushwaha
  email: kushneeraj@yahoo.com
  profession: Software Engineer
  profiles:
  - network: LinkedIn
    url: https://www.linkedin.com/in/neerajkushwaha

The simplest form of data is a key-value pair like this where the key and value are separated by a colon.

Key Value Pair - YAML
company: Jubilant FoodWorks
position: Group Engineering Manager
department: Engineering

JSON Equivalent
{
  "company": "Jubilant FoodWorks",
  "position": "Group Engineering Manager",
  "department": "Engineering"
}

In the example above, the key is the company and the value is Jubilant FoodWorks, like the rest. YAML requires a blank space after the colon to separate key and value.

We will look at how to represent arrays/lists in YAML.

Array or List - YAML
languages:
- C
- Java
- JavaScript
- Python

JSON Equivalent
{
  "languages": [
    "C",
    "Java",
    "JavaScript",
    "Python"
  ]
}

The above example shows that the languages array is followed by a colon and a new line for each language. An array element is indicated by a dash.

Let’s now look at how to represent Dictionary/Map in YAML.

A dictionary is a collection of properties organized under an item.

Dictionary or Map - YAML
java:
 version: 17.0.1
 oops: true
 developedBy: James Gosling

JSON Equivalent
{
  "java": {
    "version": "17.0.1",
    "oops": true,
    "developedBy": "James Gosling"
  }
}

The above example shows properties of the Java programming language such as version, OOPS & developed by which will differ for each language. You should have an equal number of blank spaces before each property, so they are all aligned together.

Now let’s look at YAML’s list of dictionaries. Now we are going to combine the dictionary and the list we learned so far.

List of Dictionaries - YAML
languages:
- java:
    version: 17.0.1
    oops: true
    developedBy: James Gosling
- python:
    version: 3.10.1
    oops: true
    developedBy: Guido van Rossum

JSON Equivalent
{
  "languages": [
    {
      "java": {
        "version": "17.0.1",
        "oops": true,
        "developedBy": "James Gosling"
      }
    },
    {
      "python": {
        "version": "3.10.1",
        "oops": true,
        "developedBy": "Guido van Rossum"
      }
    }
  ]
}

Note: Dictionaries are unordered whereas lists are ordered.

java:
version: 17.0.1
oops: true
developedBy: James Goslingjava:
version: 17.0.1
developedBy: James Gosling
oops: true

Although their properties are not in the same order, both dictionaries are equal in our example.

languages:
- C
- Java
- JavaScript
- Python

languages:
- C
- JavaScript
- Python
- Java

The two lists in the above example are not equal because the order matters.

Language is incomplete without comments. Any text after # is considered a comment.

# A single line comment example  

# block level comment example 
# comment line 1 
# comment line 2 
# comment line 3

Difference between JSON and YAML

They are one and the same thing, we can represent the same data almost the same way in either format.

  • YAML uses indentation to group data into lists and dictionaries, whereas JSON uses braces or curly brackets.
  • Properties are defined with the same indentation, form a dictionary in YAML, whereas curly brackets are used in JSON.
  • As opposed to YAML, where a dash is used to denote an item within a list, JSON uses square brackets to define a list, and each item within the list is separated by a comma.

You can easily convert between JSON and YAML online using https://www.json2yaml.com

Scaler Types

num1: 1          # integer
num2: 1.234 # float
str1: 'abc' # string
str2: "abc" # string
str3: abc # string
bool: false # boolean type
date: 2022-01-01 # date type
inf1: .inf # infinity
nan1: .NaN # not a number

Inheritance

YAML also has an inheritance. Take a look at the example below where we inherit from server defaults and override the port value in ssh.

YAML
serverDefaults: &server
 ip: 127.0.0.1
 port: 8080
 
ssh:
 <<: *server
 port: 22

JSON Equivalent
{
  "serverDefaults": {
    "ip": "127.0.0.1",
    "port": 8080
  },
  "ssh": {
    "ip": "127.0.0.1",
    "port": 22
  }
}

Reference (Alias indicators)

There are references in YAML that you can use within the configuration.

YAML
values: &ref
  - Will be
  - reused below
  
other_values:
  i_am_ref: *ref

JSON Equivalent
{
  "values": [
    "Will be",
    "reused below"
  ],
  "other_values": {
    "i_am_ref": [
      "Will be",
      "reused below"
    ]
  }
}

Variables using Alias indicators

YAML
greeting1: &VAR_NAME hello
greeting2: *VAR_NAME

JSON Equivalent
{
  "greeting1": "hello",
  "greeting2": "hello"
}

Multi-line strings

YAML
description: |
 I am a
 software engineer

JSON Equivalent
{
  "description": "I am a\nsoftware engineer"
}

Folded strings

YAML
description: >
 I am a
 software engineer

JSON Equivalent
{
  "description": "I am a software engineer"
}

This has been a quick overview of how to understand and write YAML configuration files.

Leave a Reply

Your email address will not be published.