Python Project 7: Read & Write JSON Files
✅ Python Project 7: Read & Write JSON Files 🔄📄
📌 Task:
Write a Python program that reads a JSON file, makes some changes, and then saves it into a new JSON file.
🧠 What You’ll Learn:
-
How to use Python’s
jsonmodule -
How to read & write JSON files
-
How to work with nested JSON data
import json
# Step 1: Read JSON file
with open("data.json", "r") as f:
data = json.load(f)
print("Original Data:")
print(data)
# Step 2: Modify data (example: add a new key-value pair)
data["status"] = "processed"
# Step 3: Save updated data into a new JSON file
with open("updated_data.json", "w") as f:
json.dump(data, f, indent=4)
print("\nUpdated JSON saved successfully!")
📥 How It Works:
-
Reads
data.jsonfile -
Adds/updates data (e.g.,
status = "processed") -
Saves changes into
updated_data.json
🔧 Try Yourself:
-
Loop through a list in JSON
-
Extract values from nested data
-
Combine multiple JSON files into one
💡 Where You Can Use It:
-
Saving API responses
-
Updating config files
-
Storing user data in apps
Comments
Post a Comment