JSON Write

Write a Python dictionary as JSON into a file.

PythonBeginner
Python
# Program to write JSON to a file

import json

data = {
    "name": "Alice",
    "age": 25,
    "skills": ["Python", "SQL"]
}

filename = input("Enter JSON filename to write: ")

with open(filename, "w", encoding="utf-8") as f:
    json.dump(data, f, indent=2)

print("JSON data written to", filename)

Output

Enter JSON filename to write: config.json
JSON data written to config.json

json.dump serializes Python objects to JSON text and writes them to a file with pretty indentation.