JSON Read

Read a JSON file into a Python dictionary using the json module.

BeginnerTopic: File Handling Programs
Back

Python JSON Read Program

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

Try This Code
# Program to read JSON from a file

import json

filename = input("Enter JSON filename: ")

try:
    with open(filename, "r", encoding="utf-8") as f:
        data = json.load(f)
        print("Loaded JSON object:", data)
except FileNotFoundError:
    print("JSON file not found.")
except json.JSONDecodeError:
    print("Invalid JSON format.")
Output
Enter JSON filename: config.json
Loaded JSON object: {'debug': True, 'version': 1}

Understanding JSON Read

json.load parses the file contents into native Python objects (dicts, lists, etc.).

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