{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "source": [ "import json\n", "\n", "# Correct file path (ensure the file is in the current working directory or provide the absolute path)\n", "file_name = 'json_file.txt' # Make sure the file name doesn't have spaces\n", "\n", "try:\n", " # Open and load the JSON file\n", " with open('/json_file.txt', 'r') as f:\n", " data = json.load(f)\n", " print(\"JSON data loaded successfully:\")\n", " print(json.dumps(data, indent=4)) # Pretty print the JSON data\n", "\n", "except FileNotFoundError:\n", " print(f\"Error: The file '{'/json_file.txt','r'}' was not found.\")\n", "except json.JSONDecodeError as e:\n", " print(f\"Error: Failed to decode JSON - {e}\")\n", "except TypeError as e:\n", " print(f\"Error: Type error - {e}\")\n", "except Exception as e:\n", " print(f\"An unexpected error occurred: {e}\")\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "KDIqroKahB8v", "outputId": "a0193862-4e27-4711-f193-1528fc29d925" }, "execution_count": 6, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "JSON data loaded successfully:\n", "{\n", " \"employees\": [\n", " {\n", " \"name\": \"Alice\",\n", " \"age\": 30,\n", " \"department\": \"HR\"\n", " },\n", " {\n", " \"name\": \"Bob\",\n", " \"age\": 25,\n", " \"department\": \"IT\"\n", " },\n", " {\n", " \"name\": \"Charlie\",\n", " \"age\": 35,\n", " \"department\": \"Finance\"\n", " }\n", " ]\n", "}\n" ] } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "Ni671mJYioOg" }, "execution_count": null, "outputs": [] } ] }