YAML and JSON
Understanding YAML and JSON: Simplifying Data Serialization for Modern Applications
WHAT IS YAML?
YAML (short for "YAML Ain't Markup Language"). It is a method for writing data in a format that is simple enough for people to understand and write. It is frequently applied to software application configuration files. Consider it a method of clearly and simply arranging information, with indentation demonstrating the data's structure.
ADVANTAGES OF YAML
YAML is a popular option for configuration files and data serialisation because of its many benefits:
1. Human-Readable: Both developers and non-developers can use YAML because its syntax is made to be simple for people to read and write.
Flexible Data Structures: YAML offers a wide range of data representation options by representing sophisticated data structures such as dictionaries, lists, and nested combinations of these.
Language-Agnostic: YAML is a flexible option for data transmission between various systems because it can be utilised with a wide variety of programming languages.
Whitespace Sensitivity: YAML uses indentation to represent the structure of the data, similar to Python, which helps in maintaining a clear and organized format.
Support for Comments: YAML allows comments, which can be used to add explanations or notes within the configuration files, improving readability and maintainability.
Widely Supported: Many tools and libraries support YAML, making it easy to integrate into various workflows and applications.
STRUCTURE OF YAML
YAML (YAML Ain't Markup Language) uses a simple and intuitive structure to represent data. Here are the key elements of YAML's structure:
Indentation: Indentation: The data's structure is represented by indentation in YAML. Spaces, not tabs, are used for indentation, and the degree of indentation reveals the data structure.
Key-Value Pairs: Data is represented as key-value pairs, where the key is followed by a colon and the value.
name: Adekunle Kadex age: 30
Lists: Lists are represented using a dash followed by a space. Each item in the list is on a new line with the same level of indentation.
fruits: - Apple - Banana - Cherry
Dictionaries: Dictionaries (or maps) are represented using key-value pairs, where the value can be another dictionary or a list.
person: name: Adekunle Kadex age: 30 -address: street: 1, Abamieda Close city: Ibadan state: Oyo zip-code: 200021
Comments: Comments in YAML start with a hash symbol (#) and can be placed anywhere in the file.
# This is a comment name: Adekunle Kadex # Inline comment
Multi-line Strings: Multi-line strings can be represented using the pipe (|) or greater-than (>) symbols. The pipe preserves line breaks, while the greater-than symbol folds lines into a single space.
description: | This is a multi-line string that preserves line breaks.
Here's a complete example that combines these elements:
#Personal information about myself
Personal Information:
-name: Adekunle
-age: 20
-email-address: adekunlefatunde@gmail.com
-address:
street: 1, Abamieda Close
city: Ibadan
state: Oyo
zip-code: 200021
#My hobbies
Hobbies:
-sports: playing football
-tourism: seeing new places
-leisure: socializing with friends
#My skills
Skills:
-typing:
proficiency-level: intermediate
-listening:
proficiency-level: intermediate
bio: >
Adekunle is a Cloud Engineer/DevOps
who loves to explore new technologies
and enjoys outdoor activities.
This example shows how YAML can be used to represent complex data structures in a clear and readable format. If you have any specific questions about YAML or need help with a particular YAML file, feel free to ask!
JSON
JSON is an acronym for JavaScript Object Notation.
JSON is composed of two data structures: An ordered list expressed using "[]" (square brackets) and a collection of key/value pairs which is expressed using "{}" (curly brackets).
JSON is considered a data interchange format intended for language-independent usage as a format which is easily composable and readable by humans.
The JSON file format is often used as a simpler alternative to XML, as it requires fewer characters and leads to fewer human errors.
Today, JSON use is ubiquitous in web and mobile applications to exchange data.
Both REST and GraphQL use JSON exclusively, as a way to communicate data between applications.
{
"Personal Information":
{
"name": "Kadex",
"age": 20,
"email": "adekunlefatunde@gmail.com",
"address":[
{
"street": "Suit 7, Agbabiaka Crescent",
"city": "Abada",
"state": "Lagos",
"zip code": "1000020"
}
]
},
"Hobbies":{
"sports": "playing football",
"tourism": "travelling to new places",
"leisure": "gisting with friends"
},
"Skills":{
"typing": "intermediate",
"listening": "intermediate"
}
}
YAML and JSON CHECKER
Use the link below to validate your YAML and JSON scripts
If your script is without error, you will see a “VALID YAML“ in green bar at the bottom of the script window. Likewise, if there is an error, it will point you to the line of error.
To validate your JSON script, hover your cursor on the YAML checker and select JSON checker.
WHERE TO WRITE A YAML OR JSON SCRIPT
- Open Visual Studio Code
- Select “open folder“ from file menu
It takes you into your computer where you can create a folder in any directory of your choice.
NB: This folder is where your YAML or JSON scripts will be saved.
- Choose “yes, i trust the author“ option
- Close the welcome page
- Select “New File“ and name your file with the .yml or . yaml extionsion if it is a YAML script. In the case of JSON, you give it a .json
- Begin to write your script
- Use the “ctrl + s“ to save your work at anytime.