Upload json_file__handling.ipynb
Browse files
pages/json_file__handling.ipynb
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"nbformat": 4,
|
3 |
+
"nbformat_minor": 0,
|
4 |
+
"metadata": {
|
5 |
+
"colab": {
|
6 |
+
"provenance": []
|
7 |
+
},
|
8 |
+
"kernelspec": {
|
9 |
+
"name": "python3",
|
10 |
+
"display_name": "Python 3"
|
11 |
+
},
|
12 |
+
"language_info": {
|
13 |
+
"name": "python"
|
14 |
+
}
|
15 |
+
},
|
16 |
+
"cells": [
|
17 |
+
{
|
18 |
+
"cell_type": "code",
|
19 |
+
"source": [
|
20 |
+
"import json\n",
|
21 |
+
"\n",
|
22 |
+
"# Correct file path (ensure the file is in the current working directory or provide the absolute path)\n",
|
23 |
+
"file_name = 'json_file.txt' # Make sure the file name doesn't have spaces\n",
|
24 |
+
"\n",
|
25 |
+
"try:\n",
|
26 |
+
" # Open and load the JSON file\n",
|
27 |
+
" with open('/json_file.txt', 'r') as f:\n",
|
28 |
+
" data = json.load(f)\n",
|
29 |
+
" print(\"JSON data loaded successfully:\")\n",
|
30 |
+
" print(json.dumps(data, indent=4)) # Pretty print the JSON data\n",
|
31 |
+
"\n",
|
32 |
+
"except FileNotFoundError:\n",
|
33 |
+
" print(f\"Error: The file '{'/json_file.txt','r'}' was not found.\")\n",
|
34 |
+
"except json.JSONDecodeError as e:\n",
|
35 |
+
" print(f\"Error: Failed to decode JSON - {e}\")\n",
|
36 |
+
"except TypeError as e:\n",
|
37 |
+
" print(f\"Error: Type error - {e}\")\n",
|
38 |
+
"except Exception as e:\n",
|
39 |
+
" print(f\"An unexpected error occurred: {e}\")\n"
|
40 |
+
],
|
41 |
+
"metadata": {
|
42 |
+
"colab": {
|
43 |
+
"base_uri": "https://localhost:8080/"
|
44 |
+
},
|
45 |
+
"id": "KDIqroKahB8v",
|
46 |
+
"outputId": "a0193862-4e27-4711-f193-1528fc29d925"
|
47 |
+
},
|
48 |
+
"execution_count": 6,
|
49 |
+
"outputs": [
|
50 |
+
{
|
51 |
+
"output_type": "stream",
|
52 |
+
"name": "stdout",
|
53 |
+
"text": [
|
54 |
+
"JSON data loaded successfully:\n",
|
55 |
+
"{\n",
|
56 |
+
" \"employees\": [\n",
|
57 |
+
" {\n",
|
58 |
+
" \"name\": \"Alice\",\n",
|
59 |
+
" \"age\": 30,\n",
|
60 |
+
" \"department\": \"HR\"\n",
|
61 |
+
" },\n",
|
62 |
+
" {\n",
|
63 |
+
" \"name\": \"Bob\",\n",
|
64 |
+
" \"age\": 25,\n",
|
65 |
+
" \"department\": \"IT\"\n",
|
66 |
+
" },\n",
|
67 |
+
" {\n",
|
68 |
+
" \"name\": \"Charlie\",\n",
|
69 |
+
" \"age\": 35,\n",
|
70 |
+
" \"department\": \"Finance\"\n",
|
71 |
+
" }\n",
|
72 |
+
" ]\n",
|
73 |
+
"}\n"
|
74 |
+
]
|
75 |
+
}
|
76 |
+
]
|
77 |
+
},
|
78 |
+
{
|
79 |
+
"cell_type": "code",
|
80 |
+
"source": [],
|
81 |
+
"metadata": {
|
82 |
+
"id": "Ni671mJYioOg"
|
83 |
+
},
|
84 |
+
"execution_count": null,
|
85 |
+
"outputs": []
|
86 |
+
}
|
87 |
+
]
|
88 |
+
}
|