#90DaysOfDevOps - Day 15
Table of contents
- What is YAML?
- What is JSON?
- Functions for YAML with Python -
- Functions for JSON with Python -
- Task 1: Create a Dictionary in Python and write it to a JSON File.
- Task 2: Read the JSON file services.json kept in this folder and print the service names of every cloud service provider.
- Task 3: Read the YAML file using Python, file services.yaml and read the contents to convert yaml to json.
As DevOps engineers, we should be able to parse files, be it Txt, JSON, YAML, etc. Let's study how to use YAML and JSON files with Python. But first, let's understand what are YAML and JSON.
What is YAML?
YAML (short for "YAML Ain't Markup Language") is a human-readable data serialization format.
It is often used for configuration files, but can also be used for data exchange between languages and platforms.
YAML is designed to be easy to read and write, with a syntax that is more concise and readable than other markup languages like XML or JSON.
YAML is whitespace-sensitive, meaning that indentation is used to indicate structure and hierarchy within the data.
To download YAML, use pip install pyyaml
To use PyYAML in your scripts, import it in a file- import yaml
Example:
In this example, we have a YAML file representing a person's name, age, and address. The file consists of a series of key-value pairs, where the keys are strings and the values can be any valid data type, including strings, numbers, booleans, arrays, and objects. Note that the indentation is used to indicate the structure of the data - the "address" key contains a sub-object with its key-value pairs, indented one level deeper than the parent object.
What is JSON?
JSON stands for JavaScript Object Notation.
It is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate.
JSON is a text format that is completely language-independent, meaning that it can be used to represent data structures in any programming language.
JSON data is represented as key-value pairs, with the keys being strings and the values being of various types, including strings, numbers, arrays, and objects.
JSON is a built-in library in Python, so we don't need to install it.
To use JSON in your scripts, import it in a file- import json
Example:
In this example, we have an object with three key-value pairs.
The keys are "name", "age", and "city", and the corresponding values are "Manu", 75, and "Antarctica".
Functions for YAML with Python -
To write a dictionary in a YAML file -
The
dump()
function expresses Python objects in YAML format. It accepts two arguments, data (which is a Python object) which is required, and optionally, a file to store the YAML format of the Python object.To write a list of dictionaries in a YAML file -
The
dump_all()
function is used to serialize Python objects—in order—into a single stream.To read data from a YAML file -
The
safe_load()
function is used to read YAML files with the PyYAML library. There is another function load() used to read YAML files but safe_load() is recommended.To read a YAML file with multiple blocks of YAML data -
For reading YAML files with multiple blocks of YAML data, you’ll use the
safe_load_all()
function and convert the output to a list
Functions for JSON with Python -
To convert a JSON string to a dictionary -
json.loads()
creates a new dictionary with the key-value pairs of the JSON string and it returns a new dictionary.To convert a dictionary to a JSON String -
json.dumps()
creates and returns a string with all the key-value pairs of the dictionary in JSON format. To give proper indentation, you can pass the argument "indent = any_number". You can also sort the keys in alphabetical order using "sort_keys=True/False".To read a JSON file -
json.load()
creates and returns a new Python dictionary with the key-value pairs in the JSON file.To write to a JSON file -
json.dump()
creates a JSON file with all the key-value pairs of the dictionary in JSON format. To give proper indentation, you can pass the argument "indent = any_number".
Let's complete some tasks to understand them more clearly.
Task 1: Create a Dictionary in Python and write it to a JSON File.
Task 2: Read the JSON file services.json
kept in this folder and print the service names of every cloud service provider.
Task 3: Read the YAML file using Python, file services.yaml and read the contents to convert yaml to json.
To learn more about using YAML with Python, refer to this website.
To learn more about using JSON with Python, refer to this website.