Table of contents
- π Introduction
- π What is JSON?
- π What is YAML?
- π Task -1 Create a Dictionary in Python and write it to a json File.
- π Task -2 Read a json file services.json and print the service names of every cloud service provider
- π Task -3 Read YAML file using python, file services.yaml and read the contents to convert yaml to json
π Introduction
As a DevOps Engineer, you should be able to parse files, be it txt, json, yaml, etc. Python has numerous libraries like os, sys, json, yaml etc that a DevOps Engineer uses in day-to-day tasks.
π What is JSON?
JSON is a lightweight data interchange format used for storing and exchanging data. It's easy for humans to read and write, and it's also easy for machines to parse and generate. JSON data is organized into key-value pairs. The keys are strings, and values can be strings, numbers, booleans, arrays, or nested objects.
π What is YAML?
YAML (YAML Ain't Markup Language) is a human-readable data serialization format. YAML uses indentation to represent the hierarchy and structure of data. No braces or brackets are used, making it visually clean and easy to read.
π Task -1 Create a Dictionary in Python and write it to a json File.
#! /usr/bin/python3
import json
dict ={"Number_first":"1","Number_second":"2","Number_third":"3"}
print("This is the dictionary - ",dict)
print(type(dict))
json = json.dumps(dict, indent=3)
print(json)
π Task -2 Read a json file services.json and print the service names of every cloud service provider
#! /usr/bin/python3
import json
file = open('services.json')
data = json.load(file)
print(data)
print("aws: ", data["services"] ["aws"] ["name"])
print("azure: ", data["services"] ["azure"] ["name"])
print("gcp: ", data["services"] ["gcp"] ["name"])
π Task -3 Read YAML file using python, file services.yaml and read the contents to convert yaml to json
#! /usr/bin/python3
import yaml
import json
file=open('services.yaml')
data= yaml.full_load(file)
print(data)
print("=============== json file =================")
json_file= json.dumps(data, indent=4)
print(json_file)
Β