{
"cells": [
{
"cell_type": "markdown",
"id": "28dc5e92-9eb7-48e0-ac81-79abfd0c943b",
"metadata": {},
"source": [
"## How to Handle JSON File "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "21355522-57ed-4671-b394-84a127ef7c1d",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "9d3caccb-c3bb-4f8e-9685-36760387ddfe",
"metadata": {},
"outputs": [],
"source": [
"x = '{\"Name\":[\"p1\",\"p2\"],\"Age\":[21,23]}'\n",
"## default json format simplest json"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "2f02b14a-4214-438b-adaa-a092dc4c8450",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\phane\\AppData\\Local\\Temp\\ipykernel_21116\\1267235832.py:1: FutureWarning: Passing literal json to 'read_json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'StringIO' object.\n",
" pd.read_json(x)\n"
]
},
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" p1 | \n",
" 21 | \n",
"
\n",
" \n",
" | 1 | \n",
" p2 | \n",
" 23 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age\n",
"0 p1 21\n",
"1 p2 23"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.read_json(x)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "fe868609-cbcc-4a2a-a7f6-c4ba76c83b0b",
"metadata": {},
"outputs": [],
"source": [
"x = '{1:[\"p1\",\"p2\"],\"Age\":[21,23]}' ## keys only take string datatype\n",
" ## Can't use int,float,complex etc only strings"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "219698c4-4327-4ded-a13c-02d089f2374f",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\phane\\AppData\\Local\\Temp\\ipykernel_21116\\1267235832.py:1: FutureWarning: Passing literal json to 'read_json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'StringIO' object.\n",
" pd.read_json(x)\n"
]
},
{
"ename": "ValueError",
"evalue": "Key name of object must be 'string' when decoding 'object'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[13], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread_json\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\pandas\\io\\json\\_json.py:804\u001b[0m, in \u001b[0;36mread_json\u001b[1;34m(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, precise_float, date_unit, encoding, encoding_errors, lines, chunksize, compression, nrows, storage_options, dtype_backend, engine)\u001b[0m\n\u001b[0;32m 802\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m json_reader\n\u001b[0;32m 803\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m--> 804\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mjson_reader\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\pandas\\io\\json\\_json.py:1014\u001b[0m, in \u001b[0;36mJsonReader.read\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 1012\u001b[0m obj \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_object_parser(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_combine_lines(data_lines))\n\u001b[0;32m 1013\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m-> 1014\u001b[0m obj \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_object_parser\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 1015\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdtype_backend \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m lib\u001b[38;5;241m.\u001b[39mno_default:\n\u001b[0;32m 1016\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m obj\u001b[38;5;241m.\u001b[39mconvert_dtypes(\n\u001b[0;32m 1017\u001b[0m infer_objects\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, dtype_backend\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdtype_backend\n\u001b[0;32m 1018\u001b[0m )\n",
"File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\pandas\\io\\json\\_json.py:1040\u001b[0m, in \u001b[0;36mJsonReader._get_object_parser\u001b[1;34m(self, json)\u001b[0m\n\u001b[0;32m 1038\u001b[0m obj \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 1039\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m typ \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mframe\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m-> 1040\u001b[0m obj \u001b[38;5;241m=\u001b[39m \u001b[43mFrameParser\u001b[49m\u001b[43m(\u001b[49m\u001b[43mjson\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mparse\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 1042\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m typ \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mseries\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mor\u001b[39;00m obj \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m 1043\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(dtype, \u001b[38;5;28mbool\u001b[39m):\n",
"File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\pandas\\io\\json\\_json.py:1173\u001b[0m, in \u001b[0;36mParser.parse\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 1172\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mparse\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m-> 1173\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_parse\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 1175\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m 1176\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n",
"File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\pandas\\io\\json\\_json.py:1366\u001b[0m, in \u001b[0;36mFrameParser._parse\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 1362\u001b[0m orient \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39morient\n\u001b[0;32m 1364\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m orient \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcolumns\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[0;32m 1365\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj \u001b[38;5;241m=\u001b[39m DataFrame(\n\u001b[1;32m-> 1366\u001b[0m \u001b[43mujson_loads\u001b[49m\u001b[43m(\u001b[49m\u001b[43mjson\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mprecise_float\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprecise_float\u001b[49m\u001b[43m)\u001b[49m, dtype\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 1367\u001b[0m )\n\u001b[0;32m 1368\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m orient \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msplit\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[0;32m 1369\u001b[0m decoded \u001b[38;5;241m=\u001b[39m {\n\u001b[0;32m 1370\u001b[0m \u001b[38;5;28mstr\u001b[39m(k): v\n\u001b[0;32m 1371\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m k, v \u001b[38;5;129;01min\u001b[39;00m ujson_loads(json, precise_float\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprecise_float)\u001b[38;5;241m.\u001b[39mitems()\n\u001b[0;32m 1372\u001b[0m }\n",
"\u001b[1;31mValueError\u001b[0m: Key name of object must be 'string' when decoding 'object'"
]
}
],
"source": [
"pd.read_json(x)"
]
},
{
"cell_type": "markdown",
"id": "aee864df-c2ec-4af4-a713-a7f055cf6022",
"metadata": {},
"source": [
"simplest form of json is\n",
"\n",
"where keys -- acting as column names\n",
"\n",
"where values --- acting as column values\n",
"\n",
"x = '{\"Name\":[\"p1\",\"p2\"],\"Age\":[21,23]}'"
]
},
{
"cell_type": "markdown",
"id": "78d82fd6-2131-479c-8d2e-1f984bf5880d",
"metadata": {},
"source": [
"### Different formats of json"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "f2a97611-b08f-4e69-bf41-6d88c08c783b",
"metadata": {},
"outputs": [],
"source": [
"x = '{\"Name\":[\"p1\",\"p2\"],\"Age\":[21,23]}'"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "1c22bf02-d8ac-4db6-a63d-6d0080f32f1a",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\phane\\AppData\\Local\\Temp\\ipykernel_21116\\1694729012.py:1: FutureWarning: Passing literal json to 'read_json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'StringIO' object.\n",
" data=pd.read_json(x)\n"
]
}
],
"source": [
"data=pd.read_json(x)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "7c10bb05-0e33-4269-81dd-75a721e10b3c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" p1 | \n",
" 21 | \n",
"
\n",
" \n",
" | 1 | \n",
" p2 | \n",
" 23 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age\n",
"0 p1 21\n",
"1 p2 23"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "3a58ef44-7f9e-402d-bfd7-770a46eaea2c",
"metadata": {},
"outputs": [],
"source": [
"x2=data.to_json(orient=\"index\")"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "34766414-bd0b-4a5e-b278-37b31471ea7b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{\"0\":{\"Name\":\"p1\",\"Age\":21},\"1\":{\"Name\":\"p2\",\"Age\":23}}'"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x2"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "a49cf619-73d0-4c6a-9716-a65020e13b7a",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\phane\\AppData\\Local\\Temp\\ipykernel_21116\\1222398037.py:1: FutureWarning: Passing literal json to 'read_json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'StringIO' object.\n",
" pd.read_json(x2,orient=\"index\")\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" p1 | \n",
" 21 | \n",
"
\n",
" \n",
" | 1 | \n",
" p2 | \n",
" 23 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age\n",
"0 p1 21\n",
"1 p2 23"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.read_json(x2,orient=\"index\")"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "6bc7744c-2bfc-48c4-9b39-61528800d00d",
"metadata": {},
"outputs": [],
"source": [
"x3 =data.to_json(orient=\"columns\")"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "86423e62-8dac-491e-ac5d-d4b9c2ea74b0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{\"Name\":{\"0\":\"p1\",\"1\":\"p2\"},\"Age\":{\"0\":21,\"1\":23}}'"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x3"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "e423c198-c578-4348-b085-c475126a354b",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\phane\\AppData\\Local\\Temp\\ipykernel_21116\\2742886167.py:1: FutureWarning: Passing literal json to 'read_json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'StringIO' object.\n",
" pd.read_json(x3,orient=\"columns\")\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" p1 | \n",
" 21 | \n",
"
\n",
" \n",
" | 1 | \n",
" p2 | \n",
" 23 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age\n",
"0 p1 21\n",
"1 p2 23"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.read_json(x3,orient=\"columns\")"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "4be204f3-2bd7-44a5-9be6-c9dc59de9c34",
"metadata": {},
"outputs": [],
"source": [
"x4 = data.to_json(orient=\"values\")"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "69aebd67-53ad-4165-8c6b-51a9c8eb0c72",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'[[\"p1\",21],[\"p2\",23]]'"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x4"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "84861077-1022-40f1-b498-c29589ad73da",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\phane\\AppData\\Local\\Temp\\ipykernel_21116\\2664431613.py:1: FutureWarning: Passing literal json to 'read_json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'StringIO' object.\n",
" pd.read_json(x4,orient=\"values\")\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 0 | \n",
" 1 | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" p1 | \n",
" 21 | \n",
"
\n",
" \n",
" | 1 | \n",
" p2 | \n",
" 23 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 0 1\n",
"0 p1 21\n",
"1 p2 23"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.read_json(x4,orient=\"values\")"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "2d7f9405-52d3-463f-9b64-661cfebe7f5f",
"metadata": {},
"outputs": [],
"source": [
"x5=data.to_json(orient=\"split\")"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "61854715-9985-45d7-82d5-219738238c4c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{\"columns\":[\"Name\",\"Age\"],\"index\":[0,1],\"data\":[[\"p1\",21],[\"p2\",23]]}'"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x5"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "0202339c-e171-4e98-824b-bfc551cad385",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\phane\\AppData\\Local\\Temp\\ipykernel_21116\\426387458.py:1: FutureWarning: Passing literal json to 'read_json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'StringIO' object.\n",
" pd.read_json(x5,orient=\"split\")\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" p1 | \n",
" 21 | \n",
"
\n",
" \n",
" | 1 | \n",
" p2 | \n",
" 23 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age\n",
"0 p1 21\n",
"1 p2 23"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.read_json(x5,orient=\"split\")"
]
},
{
"cell_type": "markdown",
"id": "417984db-6e36-44d2-89bb-097dae27edbd",
"metadata": {},
"source": [
"### Semi-Structured Json file"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "0bdd7908-27f7-43ae-9d82-ee8617f0dcf3",
"metadata": {},
"outputs": [],
"source": [
"x = {\"Name\":\"p1\",\"marks\":21} #semi_structure takes the format in dictionary\n",
" # whereas structured json takes the format in string"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "e09e79a7-6e65-40e3-8adf-cafa4365235e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Name': 'p1', 'marks': 21}"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "aa1a3330-ed5e-46b4-b910-65f8d4dfb98c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(x)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "01f92058-ff34-44a3-9d5b-08dfa5c96ab2",
"metadata": {},
"outputs": [],
"source": [
"x = {\"Name\":\"p1\",\"marks\":{\"Maths\":21,\"Hindi\":20}}"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "9cd606ed-9eca-4a10-a660-6871411c08cc",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" marks.Maths | \n",
" marks.Hindi | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" p1 | \n",
" 21 | \n",
" 20 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name marks.Maths marks.Hindi\n",
"0 p1 21 20"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.json_normalize(x)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "b33b31da-2fdf-42de-8420-e3aa534a0f15",
"metadata": {},
"outputs": [],
"source": [
"x = {\"Name\":\"p1\",\"marks\":{\"Sem1\":{\"Maths\":21,\"Hindi\":20},\"Sem2\":{\"Maths\":22,\"Hindi\":25}}}"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "2f963f46-bfea-4228-b582-315b3c69b204",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" marks.Sem1.Maths | \n",
" marks.Sem1.Hindi | \n",
" marks.Sem2.Maths | \n",
" marks.Sem2.Hindi | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" p1 | \n",
" 21 | \n",
" 20 | \n",
" 22 | \n",
" 25 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name marks.Sem1.Maths marks.Sem1.Hindi marks.Sem2.Maths marks.Sem2.Hindi\n",
"0 p1 21 20 22 25"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.json_normalize(x)"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "3909cf20-5a69-4374-bcbb-c0b60566b2fc",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" marks | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" p1 | \n",
" {'Sem1': {'Maths': 21, 'Hindi': 20}, 'Sem2': {... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name marks\n",
"0 p1 {'Sem1': {'Maths': 21, 'Hindi': 20}, 'Sem2': {..."
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.json_normalize(x,max_level=0)"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "5bf94916-44e4-4e2e-909c-fba2f241b8d3",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" marks.Sem1 | \n",
" marks.Sem2 | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" p1 | \n",
" {'Maths': 21, 'Hindi': 20} | \n",
" {'Maths': 22, 'Hindi': 25} | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name marks.Sem1 marks.Sem2\n",
"0 p1 {'Maths': 21, 'Hindi': 20} {'Maths': 22, 'Hindi': 25}"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.json_normalize(x,max_level=1)"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "4f1595f1-4602-4126-9d96-5637cd5139ec",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" marks.Sem1.Maths | \n",
" marks.Sem1.Hindi | \n",
" marks.Sem2.Maths | \n",
" marks.Sem2.Hindi | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" p1 | \n",
" 21 | \n",
" 20 | \n",
" 22 | \n",
" 25 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name marks.Sem1.Maths marks.Sem1.Hindi marks.Sem2.Maths marks.Sem2.Hindi\n",
"0 p1 21 20 22 25"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.json_normalize(x,max_level=2)"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "80e39602-f792-4f76-82b7-956125ec2101",
"metadata": {},
"outputs": [],
"source": [
"x = {\"Name\":{\"p1\":12},\"marks\":[{\"Sem1\":20,\"Sem2\":30}],\"Age\":{\"a\":{\"b\":1}}}"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "53a1d40b-a256-46c4-bc3d-0d212cea9a69",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Sem1 | \n",
" Sem2 | \n",
" Name.p1 | \n",
" Age.a.b | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 20 | \n",
" 30 | \n",
" 12 | \n",
" 1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Sem1 Sem2 Name.p1 Age.a.b\n",
"0 20 30 12 1"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.json_normalize(x,record_path=[\"marks\"],meta=[[\"Name\",\"p1\"],[\"Age\",\"a\",\"b\"]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f67c13e3-ef66-49ea-a7b0-b8e62f7b945d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}