Saturday, November 9, 2024

Demonstration Coding: Full Coding Demonstration of json.dump() and json.load()

Python programming illustration

    Good day, everyone! I hope you're all in great health and spirits. Today, I have another demonstration for you, focusing on reading and writing JSON files. I often use JSON in my programs as a database or configuration file, and I’ll show you how I make the most of it. 

Let’s dive in!


Into The Code

This module serves as a way to load and provide dictionary data from a JSON file, specifically containing paths of source and destination locations for backups. In this demonstration, the module reads paths from the JSON file and makes them accessible as a dictionary for the rest of the program.

First thing first we import the modules.

import json
from pathlib import Path

I consistently use a storage class to organize all my configurations in a structured way, making them easier to manage and maintain over time.

StorageData class

ince my coding standards follow the MVC philosophy, the CRUD operations should be located within the storage class.

Method to read and write file JSON

In this code, I’m reading a JSON file containing backup paths, if there’s an issue opening or reading the file, a custom error message is raised, making it easier to identify any issues.

Reading the paths from JSON file

For all  the complete demonstration I've made a video that you can review yourself.

Demonstration vido of json.dump() and json.load()


End

Thank you very much for visiting and reviewing my blog! I look forward to seeing you again in the next post.