Table of contents
- 🏅JSON and YAML in Python
- 🏅Advantages of JSON:
- 🏅Advantages of YAML:
- 🏅Python has numerous libraries like os, sys, json, yaml etc that a DevOps Engineer uses in day to day tasks.
- 🏅Task 1: Create a Dictionary and Write to a JSON File.
- 🏅Task 2: Read a .json file services.json kept in this folder and print the service names of every cloud service provider.
- 🏅Task 3: Read a YAML File and Convert it to JSON
🏅JSON and YAML in Python
As a DevOps Engineer you should be able to parse files, be it txt, json, yaml, etc. You should know what all libraries one should use in Python for DevOps. Python has numerous libraries like os, sys, json, yaml etc that a DevOps Engineer uses in day to day tasks. DevOps Engineers frequently work with various file formats including JSON and YAML for configuration management, automation, and data exchange tasks.
🏅Advantages of JSON:
Simplicity: JSON has a simple and straightforward syntax, making it easy for both humans to read and write, and for machines to parse and generate.
Widespread Support: JSON is widely supported across different programming languages and platforms, making it an excellent choice for interoperability and data exchange between systems.
Data Types: JSON supports a limited set of data types, including strings, numbers, Booleans, arrays, and objects, which makes it suitable for many common use cases.
Standardization: JSON has a standardized format and parsing rules, ensuring consistency in how data is represented and processed across different systems.
🏅Advantages of YAML:
Human-Readable: YAML is designed to be easily readable and writable by humans, with a syntax that closely resembles natural language. This makes it particularly well-suited for configuration files and other scenarios where human readability is important.
Expressiveness: YAML provides a more expressive syntax than JSON, allowing for more complex data structures and inline comments, which can improve readability and maintainability of configuration files.
Data Serialization: YAML supports a wider range of data types compared to JSON, including null values, dates, and complex data structures like lists of dictionaries. This flexibility makes it suitable for a broader range of use cases.
Multi-Language Support: While YAML is primarily used in the context of Python and other scripting languages, it is also commonly used in other programming environments, such as configuration files for Kubernetes and Ansible playbooks.
🏅Python has numerous libraries like os
, sys
, json
, yaml
etc that a DevOps Engineer uses in day to day tasks.
os: The
os
module provides a portable way of using operating system-dependent functionality, such as file operations, directory manipulation, and environment variables.sys: The
sys
module provides access to system-specific parameters and functions, including interaction with the Python interpreter and command-line arguments.json: The
json
module allows for parsing JSON data and encoding Python objects into JSON format, which is commonly used for data exchange and configuration files.yaml (PyYAML): The
yaml
module (often used in conjunction with PyYAML library) enables parsing and serialization of YAML data, which is commonly used for configuration files and data serialization.
🏅Task 1: Create a Dictionary and Write to a JSON File.
#First step is make .json file named as a "service.json"
{
"aws": "ec2",
"azure": "VM",
"gcp": "compute engine"
}
🏅Task 2: Read a .json file services.json
kept in this folder and print the service names of every cloud service provider.
#Second Step is make "services_json.py" file
import json
with open('services.json', 'r') as f:
services = json.load(f)
print("Service Names for Each Cloud Provider:")
for provider, service in services.items():
print(f"{provider}: {service}")
# For Showing Output open terminal & write "python services_json.py"
🏅Task 3: Read a YAML File and Convert it to JSON
To read a YAML file using Python and convert it to JSON, you can use the pyyaml library, which is commonly used for working with YAML data. First, you need to install the library using pip:
pip install pyyaml
Now, let's read a YAML file and convert it to JSON:
import yaml
import json
with open('services.yaml', 'r') as yaml_file:
yaml_data = yaml.safe_load(yaml_file)
# Convert YAML data to JSON
json_data = json.dumps(yaml_data, indent=2)
print(json_data)
In this code, we read a YAML file using yaml.safe_load and then use json.dumps to convert the YAML data to JSON format.
I'm confident that this article will prove to be valuable, helping you discover new insights and learn something enriching.🏆
Thank you : )