From Hardcoded Lists to JSON Files: Giving Flask Apps Real Data | Python Comeback Journey #8

From Hardcoded Lists to JSON Files: Giving Flask Apps Real Data | Python Comeback Journey #8

Flask app code showing json.load() and json.dump() functions inside a dark editor.
Using JSON files lets your Flask app handle data dynamically without touching the source code.

From Hardcoded Lists to JSON Files: Giving Flask Apps Real Data | Python Comeback Journey #8

Up to this point, my little Flask app had been running on hardcoded data — a list of books sitting directly inside app.py. It worked fine, but something about it felt wrong. Each time I wanted to add a new book, I had to edit the source code, save, and restart the server.

That’s not how real apps behave. Real apps load data dynamically.

So I decided to move my book data into a JSON file, and this simple change taught me more about data handling than I expected.


🧩 Step 1: Moving the Data Out

Originally, my data lived right inside the code:

I moved this into a new file inside a data/ folder:

Now, app.py no longer stored any of the actual data — it just loaded it.


⚙️ Step 2: Loading Data with json.load()

Inside the route, I replaced the hardcoded list with:

json.load() turns the file’s contents into a Python list of dictionaries — the same structure my app was already using.

Suddenly, the app became dynamic. I could edit the JSON file, refresh the page, and see the changes instantly.


🧠 Step 3: Understanding open() and File Modes

To make this work, I had to understand the role of 'r' in the open() function.

'r' means read mode — open the file to read its contents but not modify them.

Later, I learned that 'w' overwrites a file (used when saving changes), 'a' appends new content, and 'r+' allows both reading and writing. These tiny letters are small but mighty.

They taught me that file operations are explicit — Python doesn’t guess your intentions. You have to tell it exactly what kind of access you want.


📚 Step 4: Writing Back with json.dump()

When I eventually needed to modify the list and save it again (like adding a new book), I used:

The indent=4 makes the output neat and readable, which matters when you’re managing data manually.


💡 Step 5: Reflection

What started as a small refactor became a major turning point. Moving data out of the codebase introduced a new layer of separation — logic in one place, data in another.

It made me realize that managing data is really about managing change. Code changes should require redeploying the app; data changes shouldn’t.

And JSON, with its simplicity, was the perfect stepping stone toward databases.

Next up, I’ll take the idea further by learning how Flask handles form submissions — reading user input, saving it to JSON, and creating dynamic updates.

Stay tuned for Python Comeback Journey #9: Creating Forms and Handling POST Requests in Flask.

Author

  • Naoman Saeed

    I’m a self-taught developer building my way from code experiments to full-stack web solutions. At trogdyne.com, I share what I learn — from Flask and Docker to the realities of running a one-person digital agency in Pakistan.

Leave a Reply

Your email address will not be published. Required fields are marked *

Naoman

Saeed

I am a full stack web developer and technical writer passionate about MERN stack, self hosting & System thinking. This blog is my public notebook.