How to Read and Write JSON Files in Python
- Category Scripts
- Type Script
- Platform Cross-platform
- Language Python
- Price Free
- Copy 7 426
- Comments 0
How to Read and Write JSON Files in Python
import json
# Data to be written to the JSON file
data_to_save = {
"site_name": "SnippetShare",
"is_active": True,
"features": ["Copy", "Paste", "SEO"]
}
# 1. Writing data to a JSON file
with open("config.json", "w", encoding="utf-8") as file:
json.dump(data_to_save, file, indent=4, ensure_ascii=False)
# 2. Reading data back from the JSON file
with open("config.json", "r", encoding="utf-8") as file:
loaded_data = json.load(file)
print(loaded_data["site_name"])
Free snippet — copy & paste!
Copy this snippet for free on Clayi Code. One click — no account required.


There are no comments yet :(