Unlocking the Power of YAML: Syntax, Data Types, and Best Practices
Introduction to YAML Syntax, Properties, and Data Types
YAML: Yet Another Markup Language
Formerly an acronym for "Yet Another Markup Language," YAML has since evolved to humorously stand for "YAML Ain't Markup Language." It is a data format used for exchanging information and is comparable to XML and JSON, providing simple, human-readable data types.
In YAML, you can exclusively store data, steering clear of incorporating commands.
Data Serialization: This is the process of converting data objects (combinations of code and data, present in complex data structures) into a stream of bytes. The opposite process, deserialization, involves converting a stream of bytes back into objects.
Data Serialization Languages:
YAML, JSON, and XML are examples of data serialization languages. YAML is particularly popular in configuration files for tools like Docker and Kubernetes, as well as in logs and caches.
Benefits of YAML:
Human-readable and simple
Strict syntax with indentation importance
Easily convertible to JSON, XML
Widely adopted across programming languages
More powerful when representing complex data
Various tools available, including parsers
Demonstration:
YAML files typically have a .yaml
or .yml
extension. YAML is case-sensitive, and multi-line comments are not supported, only single-line comments with #
.
Storing Data:
Key-value pairs (e.g.,
"apple": "I am a red fruit"
or similar to JSON:{"apple": "I'm an apple", "mango": "I am a mango"}
).Lists (e.g.,
- Apple
,- mango
,- Apple
or similar to JSON:cities: [delhi, hyd]
).Differentiating document blocks by three hyphens (
---
).Ending YAML with three dots (
...
).
Data Types:
Variables (e.g.,
myself: Akash
).Strings (
fruit: "apple"
orjob: 'SWE'
).Writing a single line in multiple lines (using
>
).Numbers (
number: 5434
for integers,marks: 98.34
for floats).Boolean values (
booleanValue: No
orbooleanValue: y
).Specifying types (e.g.,
zero: !!int 0
,positivenum: !!int 45
,booleanVal: !!bool y
).Floating-point numbers (
marks: !!float 56.78
,infinite: !!float .inf
,not a num: .nan
).Null values (
surname: !!null Null
orsurname: !!null NULL ~
).Dates and times (
date: !!timestamp 2001-12-23
,indiaTime: 2002-12-23T02:59:43.10 +5:30
).
Advanced-Data Types:
Lists (
student: !!seq [marks, name, rollno]
).Maps (
name: Akash, role: age: 22, job: se
).Pairs (keys may have duplicate values).
Sets (
names: !!set [jaime, akash]
).Dictionaries (
people: !!omap [abhi: name: Abhishek m age: 78 height: 734, rahul: name: Rahul hulawale age: 43 height: 543]
).Reusing properties using anchors (
likings: &likes fav fruit: mango, dislikes: grapes
).Copying values from anchors (
person1: name: kunal <<: *likes, person2: name: abhi <<: *likes, person3: name: rahul <<: *likes dislikes: berries
).
This comprehensive YAML course covers its syntax, properties, and various data types, providing a solid foundation for utilizing YAML in your projects.
Happy reading :)