JSON Read

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

PythonBeginner
Python
# 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}

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