Today, let’s dive into a crucial aspect of being a DevOps Engineer, reading JSON and YAML in Python.
Python is a versatile programming language with an extensive collection of libraries that make it easier to develop various applications, from web development to data analysis, and more
☀ JSON in Python :
JSON stands for “JavaScript Object Notation”, A JSON file is a file format used for storing and exchanging data.
JSON files are composed of key-value pairs, where the keys are strings and the values can be strings, numbers, arrays, objects, booleans, or null.
It’s pretty easy to load a JSON object in Python. Python has a built-in package called JSON, which can be used to work with JSON data. It’s done by using the JSON module, which provides us with a lot of methods which among loads() and load() methods are gonna help us to read the JSON file.
import json # JSON to Python object json_data = '{"name": "John", "age": 30}' python_obj = json.loads(json_data) print(python_obj) # Output: {'name': 'John', 'age': 30} # Python object to JSON python_obj = {'name': 'John', 'age': 30} json_data = json.dumps(python_obj) print(json_data) # Output: {"name": "John", "age": 30}
☀ YAML in Python:
YAML(Yet Another Markup Language), stores the configuration file data in a serialized manner and is often used in data storage or transmission.
The
pyyaml
library in Python allows you to work with YAML data.The yaml.load() method is used to read the YAML file. This method parses and converts the YAML object to a Python dictionary so that we can read the content easily.
To convert a Python object to a YAML string, you can use the
yaml.dump()
function.import yaml # YAML to Python object yaml_data = ''' name: John age: 30 # Python object to YAML python_obj = {'name': 'John', 'age': 30} yaml_data = yaml.dump(python_obj) print(yaml_data) # Output: "age: 30 name: John"
Tasks1:
Create a Dictionary in Python and write it to a JSON File.
Let’s start by creating a Python dictionary and writing it to a JSON file. JSON provides an organized and human-readable structure for data exchange.
TASK-2
Read YAML file using Python, file
services.yaml
and read the contents to convert yaml to JSON.Now, we’ll read a JSON file, “services.json,” and extract the service names of each cloud service provider. The content of “services.json” is shown below:
{ "services": { "aws": { "name": "EC2" }, "azure": { "name": "VM" }, "gcp": { "name": "Compute Engine" } } }
Make sure that the file ‘services.json’ is in the same folder as the python file you created, otherwise, you’ll get a file not found error when trying to read the json file.
TASK3:
Read YAML file using python, file services.yaml
and read the contents to convert yaml to json:
YAML is another popular data serialization format. Let’s read “services.yaml” and convert its contents to JSON.
---
services:
aws:
name: EC2
azure:
name: VM
gcp:
name: Compute Engine
Thank you for reading. please do share and click the like👍 button below to show your support.
Happy Learning🙂!!!