Day 15 : Python Libraries for DevOps

Reading JSON and YAML in Python:

JSON (JavaScript Object Notation) and YAML (YAML Ain't Markup Language) are both popular formats for storing and transmitting structured data. They are commonly used in configuration files, API responses, and data interchange between systems. Here's a brief explanation of each with an example:

JSON

JSON is a lightweight data interchange format inspired by JavaScript object literal syntax. It's easy for both humans and machines to read and write. JSON data is represented as key-value pairs, where keys are strings and values can be strings, numbers, arrays, objects, booleans, or null.

Example of JSON:

{
  "name": "John Doe",
  "age": 30,
  "city": "New York",
  "is_student": false,
  "hobbies": ["reading", "hiking", "cooking"],
  "address": {
    "street": "123 Main St",
    "zipcode": "10001"
  }
}

YAML

YAML is a human-readable data serialization format that aims to be easily readable by humans and easily parseable by machines. It uses indentation to represent data structures and relies on whitespace indentation to denote nesting. YAML supports complex data structures such as arrays, dictionaries, and nested objects.

Example of YAML:

name: Yashraj singh sisodiya
age: 22
city: indore
is_student: false
hobbies:
  - reading
  - hiking
  - cooking
address:
  street: sundar nagar extension
  zipcode: '456010'

In Python, you can use the json and yaml libraries to work with JSON and YAML data, respectively. Here's a simple example of reading JSON and YAML files in Python:

import json
import yaml

# Read JSON file
with open('data.json') as json_file:
    json_data = json.load(json_file)
    print("JSON Data:")
    print(json_data)

# Read YAML file
with open('data.yaml') as yaml_file:
    yaml_data = yaml.safe_load(yaml_file)
    print("\nYAML Data:")
    print(yaml_data)

This code snippet demonstrates how to read JSON and YAML files using Python's built-in libraries (json for JSON and yaml for YAML). The data from both files is loaded into Python dictionaries for further processing.

Tasks:

Here are three code snippets, each with comments:

parser.py

This script parses both JSON and YAML files and prints their contents:

import json
import yaml

# Define file paths
json_file = "services.json"
yaml_file = "services.yaml"

# Read and parse JSON file
with open(json_file, 'r', encoding='utf-8') as f:
    json_data = json.loads(f.read())

print("JSON:\n", json_data)

# Read and parse YAML file
with open(yaml_file, "r") as stream:
    try:
        yaml_data = yaml.safe_load(stream)
    except yaml.YAMLError as exc:
        print(exc)

JSON

This is the JSON data:

{
    "services": {
        "debug": "on",
        "aws": {
            "name": "EC2",
            "type": "pay per hour",
            "instances": 500,
            "count": 500
        },
        "azure": {
            "name": "VM",
            "type": "pay per hour",
            "instances": 500,
            "count": 500
        },
        "gcp": {
            "name": "Compute Engine",
            "type": "pay per hour",
            "instances": 500,
            "count": 500
        }
    }
}

YAML

This is the YAML equivalent of the JSON data:

services:
  debug: 'on'
  aws:
    name: EC2
    type: pay per hour
    instances: 500
    count: 500
  azure:
    name: VM
    type: pay per hour
    instances: 500
    count: 500
  gcp:
    name: Compute Engine
    type: pay per hour
    instances: 500
    count: 500

These scripts and data represent a scenario where you're reading service information from both JSON and YAML files. The parser.py script is responsible for parsing both types of files, while the JSON and YAML snippets represent the content of the respective files.

ย