File size: 2,606 Bytes
880f6de |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
{
"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": []
}
]
} |