Complete Guide To YAML

Hello Everyone,
Hope you’re doing well. Nowadays a lot of tools started using YAML format for input configurations so thought of documenting the same for quick reference. I’ll give example of POD configuration file of Kubernetes at the end. Hope it’ll help clearing concepts.

What is YAML ?
YAML is a Data Serialization Language, you might have seen XML or JSON, they are also Data Serialization Languages commonly used. Just tobe on the same page Data Serialization is a process to convert datastructure or object into a format which can be transmitted or reconstructed as and when required. I hope this gives you brief idea of YAML, let’s understand different data types supported by YAML..

Data Types in YAML
YAML works on indentation so proper indentation is required otherwise your data may throw error. Also we can store YAML file with extension as “.yml” or “.yaml”. Let’s see different data types supported by YAML. (NOTE: I’ll compare YAML with JSON as JSON is quite commonly used, if you’re not aware of JSON, please ignore comparision and directly focus on concept.)

Object (Basic Data Types)

YAML :

rollNo: 1                                // Number
name: Sanket                             // String
active: true                             // Boolean
description: "string: with colon"        // String with ":" operator

JSON :

{
    "rollNo": 1,
    "name": "Sanket",
    "active": true,
    "description": "string: with colon"
}

--> In JSON, object starts with open curly braces, where as in YAML, default type is object so we don't require any additional syntax.

Object As Value

YAML :

rollNo: 1                                // Number
name: Sanket                             // String
active: true                             // Boolean
description: "string: with colon"        // String with ":" operator
address:                                 // Object as value
    city: Pune
    State: Maharashtra
address: {city: Pune, State: Maharashtra} // Another way for object as value, but not much readable

JSON :

{
    "rollNo": 1,
    "name": "Sanket",
    "active": true,
    "description": "string: with colon"
    "address": {
        "city": "Pune",
        "State": "Maharashtra"
    }
}

Array

YAML :

rollNo: 1                                // Number
name: Sanket                             // String
active: true                             // Boolean
description: "string: with colon"        // String with ":" operator
subjects:                                // Array as value
    - Maths
    - Science
objectArray:
    - { key: value }                     // Not much readable for huge object.
    - key1: value                         // Multiple keys in single object
      key2: value
      key3: value
jsonArray: [item1, item2]                // Declaring array json type.


JSON :

{
    "rollNo": 1,
    "name": "Sanket",
    "active": true,
    "description": "string: with colon"
    "subjects": [
        "Maths",
        "Science"
    ],
    "objectArray": [
        {
            "key": "value"
        },
        {
            "key1": "value",
            "key2": "value",
            "key3": "value"
        }
    ]
}

Multiline String

YAML :

rollNo: 1                                // Number
name: Sanket                             // String
active: true                             // Boolean
description: "string: with colon"        // String with ":" operator
longText1: >                             // Complete single string.
    we can write as long text as we want
    each line will have same indentation
longText2: |                              // Retain newline character
    Each line will be end with
    new line character
    check json representation

JSON :

{
    "rollNo": 1,
    "name": "Sanket",
    "active": true,
    "description": "string: with colon"
    "longText1": "we can write as long text as we want each line will have same indentation\n",
    "longText2": "Each line will be end with\n new line character\n check json representation\n" 
}

Sample Kubernetes POD Definition

apiVersion: v1
kind: Pod
metadata:
  name: static-web
  labels:
    role: myrole
spec:
  containers:
    - name: web
      image: nginx
      ports:
        - name: web
          containerPort: 80
          protocol: TCP
Source: Kubernetes.io

That’s it for this article, hope it was helpful for you to understand YAML completely. BDW, a lot of people ask full-form of YAML, officially we see it’s “YAML Ain’t Markup Language” (as per yaml.org). In case of any question/concern, please get in touch with me. Keep learning, Stay Safe.

Sanket Modi

Working in Information Technology since 2012. Started my career as Java Developer and now working in multiple different technologies like nodejs, Python, Docker, Kubernetes, Azure etc. I like to explore new technologies and read books.

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: