JSON Write

Write a Python dictionary as JSON into a file.

BeginnerTopic: File Handling Programs
Back

Python JSON Write Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# 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

Understanding JSON Write

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

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Table of Contents