JSON Write in Python
Write a Python dictionary as JSON into a file.
BeginnerFile Handling ProgramsExample 7 of 20
json-write.py
1# Program to write JSON to a file23import json45data = {6 "name": "Alice",7 "age": 25,8 "skills": ["Python", "SQL"]9}1011filename = input("Enter JSON filename to write: ")1213with open(filename, "w", encoding="utf-8") as f:14 json.dump(data, f, indent=2)1516print("JSON data written to", filename)
Output
Enter JSON filename to write: config.json JSON data written to config.json
What's going on
json.dump serializes Python objects to JSON text and writes them to a file with pretty indentation.
After this program
If the idea is still fuzzy, read Day 14: File Handling in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 file handling programs, or see every topic on Python exercises.