Table of contents
✔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 Pythonfor DevOps.
Python has numerous libraries like
os
,sys
,json
,yaml
etc that a DevOps Engineer uses in day to day tasks.
✔Advantages of YAML:
Human- Readable and Writeable
YAML's syntax is designed to be human-friendly and easily readable, featuring indentation-based structure and minimal punctuation. This readability enhances collaboration and understanding, particularly in scenarios where non-technical stakeholders are involved.
Expressiveness and Conciseness
YAML offers a concise and expressive format for representing complex data structures, including lists, dictionaries, and nested objects.reduces verbosity and enhances readability, especially for configuration files and data serialization.
Data Serialization
YAML supports serialization of complex data types, including objects, arrays, and nested structures, without the need for additional transformation. This makes it suitable for serializing and deserializing data between different programming languages and environments.
Integration with Configuration Management
YAML is commonly used for configuration files in software development, deployment, and infrastructure management. Its human-readable syntax and support for hierarchical configurations make it well-suited for defining settings, parameters, and dependencies in applications and systems.
✔Advantages of JSON:
Simplicity and Familiarity
JSON's syntax is straightforward and closely resembles JavaScript object notation, making it intuitive and easy to understand for developers familiar with JavaScript or similar languages.
Lightweight and Fast
JSON is a lightweight format, which means it's efficient in terms of both storage and transmission. Its simplicity also contributes to faster parsing and serialization, making it ideal for use cases where performance is critical.
Widespread Adoption
JSON has gained widespread adoption across various domains, including web development, APIs, and configuration files. Its popularity ensures extensive support across programming languages and platforms, making it a versatile choice for data interchange.
Standardization
JSON has a standardized format and parsing rules, ensuring consistency in how data is represented and processed across different systems.
âš¡ In summary, while JSON excels in simplicity, performance, and interoperability, YAML stands out for its readability, expressiveness, and suitability for configuration management.
✔Tasks:
Create a Dictionary and Write to a JSON File.
import json
# Create a dictionary
data = {
"name": "Vivek",
"age": 23,
"city": "Pune"
}
# Define the file path where you want to save the JSON file
file_path = "data.json"
# Write the dictionary to a JSON file
with open(file_path, "w") as json_file:
json.dump(data, json_file)
print("Dictionary has been written to data.json successfully!")
In this code:
We create a dictionary named
data
containing some key-value pairs (in this case, the name, age, and city).We specify the file path where we want to save the JSON file (
data.json
).We open the file in write mode (
"w"
) using theopen()
function along with a context manager (with
statement) to ensure that the file is properly closed after writing.We use
json.dump()
function to write the contents of the dictionarydata
into the JSON file specified byjson_file
.Finally, we print a message indicating that the dictionary has been successfully written to the JSON file.
After running this code, you should find a file named data.json
in your current working directory, containing the dictionary data in JSON format.
Read a .json file
services.json
kept in this folder and print the service names of every cloud service provider.
import json
# Specify the path to the JSON file
file_path = "services.json"
# Read the JSON file
with open(file_path, "r") as json_file:
services_data = json.load(json_file)
# Print service names of every cloud service provider
for provider, service in services_data.items():
print(f"{provider}: {service}")
In this code:
We specify the path to the JSON file "services.json".
We use the
open()
function to open the JSON file in read mode ("r"
) along with a context manager (with
statement).We use
json.load()
function to load the contents of the JSON file into theservices_data
dictionary.We iterate through the items of the
services_data
dictionary using afor
loop, where each item represents a cloud service provider and its corresponding service name.Inside the loop, we print the provider name and its associated service name.
Make sure that the "services.json" file is in the same directory as your Python script, or you need to provide the correct path to the file.
Read a YAML File and Convert it to JSON
import yaml
import json
# Specify the path to the YAML file
yaml_file_path = "services.yaml"
# Read the YAML file
with open(yaml_file_path, "r") as yaml_file:
yaml_data = yaml.safe_load(yaml_file)
# Convert YAML data to JSON
json_data = json.dumps(yaml_data, indent=4)
# Print the JSON data
print(json_data)
In this code:
We import the
yaml
andjson
modules.We specify the path to the YAML file ("services.yaml").
We use the
open()
function to open the YAML file in read mode ("r"
) along with a context manager (with
statement).We use
yaml.safe
_load()
function to load the contents of the YAML file into theyaml_data
dictionary.safe_load()
is used to safely load YAML data without executing arbitrary code.We use
json.dumps()
function to convert theyaml_data
dictionary to a JSON-formatted string. We also specifyindent=4
to format the JSON data with indentation for better readability.Finally, we print the JSON data.
This will read the YAML file, convert its contents to JSON, and print the JSON-formatted data. You can then use this JSON data as needed, such as writing it to a JSON file or passing it to other functions for further processing.
Happy Learning😊