Day15 of #90daysofdevops | Python Libraries json and yaml

ยท

6 min read

Introduction to json and yaml

JSON (JavaScript Object Notation):

  • JSON stands for JavaScript Object Notation.

  • It is a lightweight data interchange format used for storing and exchanging data between different systems.

  • JSON is easy for both humans to read and write and for machines to parse and generate.

  • JSON is primarily used in web development for transmitting data between a server and a web application as an alternative to XML.

  • JSON data is represented in key-value pairs, where the keys are strings, and the values can be of various data types, such as strings, numbers, booleans, arrays, and even other JSON objects.

  • JSON values cannot be functions or methods; they are meant to be data only.

  • JSON objects are enclosed in curly braces {}, and each key-value pair is separated by a colon :.

  • JSON arrays are ordered lists of values, enclosed in square brackets [], with each value separated by a comma.

YAML (YAML Ain't Markup Language):

  • YAML stands for YAML Ain't Markup Language (a humorous backronym).

  • It is a human-readable data serialization format and is often used for configuration files and data exchange between different programming languages.

  • YAML is designed to be easy to read and write for humans and can be used as a configuration file without requiring complex syntax.

  • YAML data is represented using indentation and colons to define key-value pairs.

  • It supports various data types, including scalars (strings, numbers, booleans), arrays, and dictionaries (objects).

  • YAML emphasizes readability and organization through indentation, making it popular for writing configuration files.

Tasks with code explanation(step-wise)

  1. Create a Dictionary in Python and write it to a json File.

    Above code explanation:

    1. import json: This line imports the json module, which allows us to work with JSON data in Python. The json module provides methods for encoding Python objects into JSON format and decoding JSON data back into Python objects.

    2. data: This is a Python dictionary that contains some sample data. It represents information about a person named John Doe, including his name, age, email, subscription status, interests, and address.

    3. file_name = "data.json": Here, we define a variable called file_name and set it to the string "data.json". This will be the name of the JSON file where we will write the data.

    4. Writing data to JSON file:

      a. with open(file_name, "w") as json_file:: This line opens a file named data.json in write mode ("w"). The with statement is used with the file handling to ensure that the file is properly closed after its suite finishes. The file object is assigned to the variable json_file, which we can use to write data to the file.

      b. json.dump(data, json_file, indent=4): The json.dump() function is used to write the data dictionary to the JSON file json_file. It takes three arguments: the data to be written (data), the file object (json_file), and an optional argument indent with the value 4. The indent=4 argument is used to add indentation to the JSON file, making it more human-readable.

      c. The data from the data dictionary is converted to JSON format and written to the data.json file.

    5. print(f"Data has been written to {file_name} successfully."): Finally, this line prints a success message, indicating that the data has been written to the JSON file successfully.

After running this code, you will find a file named "data.json" in the same directory as your Python script. The contents of this file will be in JSON format and will match the data in the data dictionary. The JSON data will be properly indented for better readability.

  1. Read a json file services.json kept in this folder and print the service names of every cloud service provider.

    Above code explanation step-wise:

    1. Importing the required module:
    import json

The code starts by importing the json module, which provides functions for working with JSON data in Python.

  1. Storing JSON data in a string:
    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
            }
        }
    }
    '''

The JSON data is provided as a multi-line string, representing a JSON object with nested data.

  1. Parsing JSON data into a Python dictionary:
    data = json.loads(json_data)

The json.loads() function is used to parse the JSON data from the json_data string and convert it into a Python dictionary. The resulting dictionary is stored in the variable data.

  1. Extracting service names from the dictionary:
    service_names = [data["services"][provider]["name"] for provider in data["services"] if provider != "debug"]

This line of code uses a list comprehension to extract the service names of each cloud service provider from the data dictionary. It iterates through each provider (excluding the "debug" key) in the "services" dictionary and retrieves the value associated with the "name" key, which represents the service name. The service names are stored in the service_names list.

  1. Printing the service names:
    for service_name in service_names:
        print(service_name)

Finally, the code uses a for loop to iterate through the service_names list and prints each service name one by one. The output will be the names of each cloud service provider's service, i.e., "EC2", "VM", and "Compute Engine".

  1. Read YAML file using python, file services.yaml and read the contents to convert yaml to json

    Above code explanation step-wise:

    1. Import necessary modules:
    import yaml
    import json

These lines of code import the required modules, yaml and json. The yaml module is used to work with YAML data, while the json module is used to work with JSON data.

  1. Define the YAML data:
    yaml_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
    '''

This is a multi-line string that contains YAML data. YAML is a human-readable data serialization format. In this example, the YAML data represents information about various cloud services under the "services" key.

  1. Load YAML data into a Python dictionary:
    data = yaml.safe_load(yaml_data)

Here, we use the yaml.safe_load() function to convert the YAML data into a Python dictionary named data. This function safely loads YAML data into Python objects.

  1. Convert the Python dictionary to JSON format:
    json_data = json.dumps(data, indent=4)

The json.dumps() function is used to convert the Python dictionary data into a JSON-formatted string. The indent=4 parameter ensures that the JSON output is formatted with indentation, making it more human-readable.

  1. Print the JSON data:
    print(json_data)

Finally, we print the JSON data on the screen using the print() function. This will display the YAML data converted to JSON format with proper indentation.

So, overall, this code takes YAML data, converts it into a Python dictionary, and then converts that dictionary into a JSON-formatted string, which is then printed on the screen.

Thanks for reading the blog & do share them with someone in need :)

Please share your views and suggestions, they are always welcome.

See you then in the next blog.

Happy learning :)

ย