JSON File Reader

Write a Python program that reads a JSON file and prints its contents. The program should handle exceptions for scenarios like file not found, improper JSON format, etc.

Example 1:

Input: "correct_file.json"
Output: { "name": "John", "age": 30, "city": "New York" }

Example 2:

Input: "incorrect_file.json"
Output: "Exception: File not found"

Use Python’s built-in json module to read the JSON file.

import json

def read_json_file(file_name):
        with open(file_name, 'r') as json_file:
            return json.load(json_file)
    except FileNotFoundError:
        return "Exception: File not found"
    except json.JSONDecodeError:
        return "Exception: Improper JSON format"


© Let’s Data Science


Unlock AI & Data Science treasures. Log in!