DEV Community

Cover image for YAML DataTypes and Real-World Examples
Sumit Mukharjee
Sumit Mukharjee

Posted on

YAML DataTypes and Real-World Examples

This is the continuation of the YAML introduction head over here for part 1.
So let's continue with the remaining portion of YAML.
Key Datatype

Key-Value pairs

"apple" : "I am a red fruit"
1 : "This is a roll number"
Enter fullscreen mode Exit fullscreen mode

List Datatype

- apple
- mango
- banannananna
Enter fullscreen mode Exit fullscreen mode

Block Style

cities:
 - new delhi
 - mumbai
 - ahmedabad
Enter fullscreen mode Exit fullscreen mode

YAML Parsers can be used to check if YAML is valid or not.
One such website is YAMLLINT.
Differentiating Between Documents

cities:
 - new delhi
 - mumbai
 - ahmedabad
 ---
 cities: [new Delhi, mumbai]
 ---
 {mango: "fruit", age:5}
 ...
Enter fullscreen mode Exit fullscreen mode

--- is used to differentiate the type of document.
... is used to define that document is finished now.

JSON Files
Onlineyamltools is a website for conversion and stuff for YAML files.

YAML FILE 👇

cities:
 - new delhi
 - mumbai
 - ahmedabad
Enter fullscreen mode Exit fullscreen mode

converted to JSON 👇

{
  "cities": [
    "new delhi",
    "mumbai",
    "ahmedabad"
  ]
}
Enter fullscreen mode Exit fullscreen mode

Flow Type

 cities: [new Delhi, mumbai]

Enter fullscreen mode Exit fullscreen mode

This doesn't give an indentation error

Comments
YAML doesn't support multiline comments, we have to use the # sign

# Mango
# Apple
# Grapes
Enter fullscreen mode Exit fullscreen mode

String Variables

name: Sumit Mukharjee
fruit: "Apple"
job: 'swe'
message: !!str Hello There
Enter fullscreen mode Exit fullscreen mode

Write a single line in multiple lines

message: >
this will
all be 
in one single line
Enter fullscreen mode Exit fullscreen mode

same as This will all be in one single line

YAML auto detects data type

Specifying the data types

zero: !!int 0
positiveNum: !!int 45
negativeNum: !!int -45
binaryNum: !!int 0b11001
octalNum: !!int 06574
hexNum: !!int 0x45
commaValue: !!+540_000 # 540,000
exponentialNumbers: 6.023E56
Enter fullscreen mode Exit fullscreen mode

Float Value:

marks: !!float 56.89
infinity: !!float .inf
noANumber: .nan
Enter fullscreen mode Exit fullscreen mode

Boolean Value

booleanValues: !!bool No
Enter fullscreen mode Exit fullscreen mode

Null Value

surname: !!null Null
~: this is a null key
Enter fullscreen mode Exit fullscreen mode

DateTime in YAML

date: !!timestamp 2022-01-25
india time: 2001-12-15T02:59:43.10 +5:30
no time zone: 2001-12-15T02:59:43.10
Enter fullscreen mode Exit fullscreen mode

Advanced-Data Type

When some of the sequences will be empty they are known as a sparse sequence.

```sparse seq:

  • hey
  • how
  • Null
  • sup ```

Nested Sequence
sequence inside sequence

 - marks
 - name
 - roll_no
- 
 - simplemarks
 - avgMarks
Enter fullscreen mode Exit fullscreen mode

Maps DataType
key:value pairs are called map datatype
Nested Maps

```name: Sumit Mukharjee
role:
age: 78
job: student




**Pairs Datatype**
Sometimes one key may require duplicate values




```pair examples: !!pairs
 - job: student
 - job: teacher
Enter fullscreen mode Exit fullscreen mode

Real World Example

Image description
Storing Above data in XML

<?xml version="1.0" encoding="UTF-8">
<School name="DPS" principal="Someone">
  <Students>
     <Student>
          <rno>23</rno>
          <name>"Sumit"</name>
          <marks>94</marks>
        </Student>
   </Students>
</School>
Enter fullscreen mode Exit fullscreen mode

** In JSON**

{
    "School": [
        {
            "name": "DPS",
            "principal": "Someone",
            "Students": [
                {
                    "rno": 12,
                    "name": "Sumit Mukharjee",
                    "marks": 67
                }
            ]
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

In YAML

  - name: DPS
    principal: Someone
    Students:
      - rno: 12
        name: Sumit Mukharjee
        marks: 67
Enter fullscreen mode Exit fullscreen mode

This was it. Thanks for reading😊.

Top comments (0)