📌 Quick Info
- Topic: Working with JSON in Python
- Target Audience: Beginners who know files and dictionaries
- Goal: Understand how to save and load structured data
1. Introduction
"I could write text to a file. But what if I needed to save a whole dictionary? A list of users? A config file? That's when I learned about JSON — and now it's how every one of my projects saves data."
2. The Problem (Saving Complex Data to a Text File)
user = {"name": "Ali", "age": 25, "city": "Karachi"}
with open("user.txt", "w") as file:
file.write(str(user))
What’s in the file:
{'name': 'Ali', 'age': 25, 'city': 'Karachi'}
Problem: It’s ugly. It’s not standard. Other programs can’t read it easily.
3. The Solution (JSON)
import json
user = {"name": "Ali", "age": 25, "city": "Karachi"}
with open("user.json", "w") as file:
json.dump(user, file)
What’s in the file:
{"name": "Ali", "age": 25, "city": "Karachi"}
Clean. Standard. Readable by almost any language.
4. Reading JSON Back
import json
with open("user.json", "r") as file:
user = json.load(file)
print(user["name"]) # Ali
print(user["age"]) # 25
The Dictionary comes back exactly as it was saved.
5. How It Works (Line By Line)
import json — Loads Python’s built-in JSON module.
json.dump(data, file) — Writes a Python object to a file as JSON.
json.load(file) — Reads JSON from a file back into a Python object.
json.dumps(data) — Converts a Python object to a JSON string (instead of writing to a file).
json.loads(string) — Parses a JSON string into Python.
6. JSON vs Python: What Maps to What
| Python | JSON |
|---|---|
| dict | Object |
| List | Array |
| str | string |
| int/ float | number |
| True / False | true / false |
| None | null |
The names are different, but the structure is the same.
7. Real Example (My Practice)
import json
users = [
{"name": "Ali", "score": 85},
{"name": "Sara", "score": 92},
{"name": "Ahmed", "score": 78}
]
with open("scores.json", "w") as file:
json.dump(users, file, indent=2)
with open("scores.json", "r") as file:
loaded = json.load(file)
for user in loaded:
print(f"{user['name']}: {user['score']}")
Output:
Ali: 85
Sara: 92
Ahmed: 78
The indent=2 makes the file human-readable.
8. Common Mistakes Beginners Make
Mistake 1: Forgetting to import json
json.dump(...)
Mistake 2: Confusing dump and dumps
- json.dump(): writes to a file
- json.dumps(): returns a string
Mistake 3: Trying to save non-JSON types
import json
json.dumps({"date": datetime.now()})
JSON only supports basic types. Convert to strings first.
9. What I learned
-) JSON is the standard for saving structured data
-) json.dump() writes to a file, json.load() reads from it
-) json.dumps() and json.loads() work with strings
-) indent=2 makes files human-readable
-) Not everything can be saved — only dicts, lists, strings, numbers, booleans, and None
10. Conclusion
“JSON solved a problem I didn't know I had. Now every project I build saves its data as JSON — because it's readable, portable, and just works. Once you learn it, you'll never go back to saving dictionaries as text.”
Top comments (1)