Building a Simple Banking System in Python Using JSON for Data Storage

Muhammad AmjadMuhammad Amjad
2 min read

Banking systems are essential for handling financial transactions like deposits, withdrawals, and balance inquiries. In this blog, we will build a simple banking system using Python and store the data in a JSON file. Unlike databases, JSON files provide a lightweight way to manage structured data, making them ideal for small projects.

What is a JSON File?

JSON (JavaScript Object Notation) is a lightweight data format used for storing and exchanging data. It is human-readable and easy to work with in various programming languages, including Python.

Example JSON Format:

{
    "users": [
        {
            "account_number": "12345",
            "name": "John Doe",
            "balance": 1000.0
        }
    ]
}

Why Use JSON for a Banking System?

  • Lightweight: No need for a full database setup.

  • Human-readable: Easy to understand and edit manually if needed.

  • Portable: JSON files can be transferred across different systems easily.

Steps to Build a Banking System in Python Using JSON

Step 1: Import Required Libraries

We need the json module to handle JSON files.

import json
import os

Step 2: Create a JSON File for Data Storage

We initialize a bank_data.json file if it does not exist.

def initialize_json():
    if not os.path.exists("bank_data.json"):
        with open("bank_data.json", "w") as file:
            json.dump({"users": []}, file)

Step 3: Load Data from JSON File

def load_data():
    with open("bank_data.json", "r") as file:
        return json.load(file)

Step 4: Save Data to JSON File

def save_data(data):
    with open("bank_data.json", "w") as file:
        json.dump(data, file, indent=4)

Step 5: Create Functions for Banking Operations

1. Create an Account

def create_account(name, account_number, initial_balance=0.0):
    data = load_data()
    data["users"].append({
        "account_number": account_number,
        "name": name,
        "balance": initial_balance
    })
    save_data(data)
    print("Account created successfully!")

2. Deposit Money

def deposit(account_number, amount):
    data = load_data()
    for user in data["users"]:
        if user["account_number"] == account_number:
            user["balance"] += amount
            save_data(data)
            print(f"Deposited ${amount}. New Balance: ${user['balance']}")
            return
    print("Account not found!")

3. Withdraw Money

def withdraw(account_number, amount):
    data = load_data()
    for user in data["users"]:
        if user["account_number"] == account_number:
            if user["balance"] >= amount:
                user["balance"] -= amount
                save_data(data)
                print(f"Withdrawn ${amount}. Remaining Balance: ${user['balance']}")
            else:
                print("Insufficient balance!")
            return
    print("Account not found!")

4. Check Balance

def check_balance(account_number):
    data = load_data()
    for user in data["users"]:
        if user["account_number"] == account_number:
            print(f"Account Balance: ${user['balance']}")
            return
    print("Account not found!")

Testing the Banking System

initialize_json()
create_account("Alice", "11111", 500)
deposit("11111", 200)
withdraw("11111", 100)
check_balance("11111")

Conclusion

We have successfully built a basic banking system in Python using JSON for data storage. This approach is suitable for small-scale applications, but for larger systems, using a database like MySQL or MongoDB is recommended.

1
Subscribe to my newsletter

Read articles from Muhammad Amjad directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Muhammad Amjad
Muhammad Amjad