Understand YAML who already knows JSON.

Understand YAML who already knows JSON.

What is YAML?

YAML is a human-readable data serialization language that is often used for writing configuration files and in applications where data is being stored or transmitted.

Syntax

First of all YAML is also a superset of JSON, so JSON files are valid in YAML.

YAML uses Python-style indentation to indicate nesting. Tab characters are not allowed, so whitespaces are used instead. There are no usual format symbols, such as braces, square brackets, closing tags, or quotation marks.

The structure of a YAML file is a map or a list.

Key-value / map :

Maps allow you to associate key-value pairs. Each key must be unique, and the order doesn't matter. We can think of it as a JSON {key-value} pair. In JSON every key also has to be a Siring but not in YMAL.

JSON

{
    "name": "Debasish Biswas",
    "age": 23
    "edu": {
        "college": "NIT Jamshedpur",
        "deg": "post-graduation"
    }
}

YAML

name: Debasish Biswas
age: 23
edu:
    college: NIT Jamshedpur
    deg: post-graduation

array / list :

A list includes values listed in a specific order and may contain any number of items needed. A list sequence starts with a dash (-) and a space, while indentation separates it from the parent. It is equivalent to Arrays in JSON.

JSON

{
    "city": [
        "Siliguri",
        "Kolkata",
        "Delhi"
    ],
    "student": [{
            "name": "Debasish",
            "marks": 87.3
        },
        {
            "name": "Anuradha",
            "marks": 83.7
        },
        {
            "name": "Rakesh",
            "marks": 84.3
        }]
}

YAML

city:
  - Siliguri
  - Kolkata
  - Delhi
student:
  - name: Debasish
    marks: 87.3
  - name: Anuradha
    marks: 83.7
  - name: Rakesh
    marks: 84.3

As YAML is a strict superset of JSON it has more features than this.

Comments:

We can add comments to our YAML code by adding a # in front of any line.

# this is a comment that will be ignored
name: YAML

--- & ... :

The symbol --- can be seen very often in YMAL it indicates a beginning of a new Document and the symbol ... indicates of ending of the Document.

---
- India
- the USA
- Bangladesh

---
school: DPS
college: IIT
...

YAML Datatypes :

  • sequences Already covered above, sequences are simply lists or arrays.
  • mappings Already covered above, mappings are maps or key-value Pairs.
  • Scalers

Scalars are the simplest data type in YAML and can represent basic types, including String, boolean, integers, floating-point numbers, Date, and Timestamp.

string: "Hello"
integer: 123
float: 12.345
boolean: No

As we can see, the format for each data type is very similar. To represent a string in YAML, we use a single quote ' or a double quote " and the actual string value within these quotes. We use the standard numeric literals for integers and floating-point numbers. Boolean values can be represented in YAML using keywords like true and false or Yes and No.

YAML can autodetect types. However, it is often necessary to explicitly specify the type using a tag. To force a type, we can prefix the type with a !! symbol. Here's an example:

age: !!float 23.2
married: !!str true
binary: !!int 0b101010
hexadecimal: !!int 0x1C7A
name: !!str "James"
cities: !!seq
    - Siliguri
    - Kolkata
NA: !!null NULL
boolean: !!bool No

Conclution

YAML is a very versatile data serialization standard that can be used to represent many different data types.