Skip to content

Commit

Permalink
📝 Simplify the json example
Browse files Browse the repository at this point in the history
  • Loading branch information
veit committed Oct 12, 2023
1 parent 09e66ba commit 1998aad
Showing 1 changed file with 48 additions and 70 deletions.
118 changes: 48 additions & 70 deletions docs/data-processing/serialisation-formats/json/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,77 +7,37 @@
"source": [
"# JSON example\n",
"\n",
"JSON (short for _JavaScript Object Notation_) has become one of the standard formats for transmitting data via HTTP request between web browsers and other applications. Here is an example:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "49bc5693",
"metadata": {},
"outputs": [],
"source": [
"books = \"\"\"\n",
"[\n",
" {\n",
" \"Title\": \"Python basics\",\n",
" \"Language\": \"en\",\n",
" \"Authors\": \"Veit Schiele\",\n",
" \"License\": \"BSD-3-Clause\",\n",
" \"Publication date\": \"2021-10-28\"\n",
" },\n",
" {\n",
" \"Title\": \"Jupyter Tutorial\",\n",
" \"Language\": \"en\",\n",
" \"Authors\": \"Veit Schiele\",\n",
" \"License\": \"BSD-3-Clause\",\n",
" \"Publication date\": \"2019-06-27\"\n",
" }\n",
"]\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"id": "cbf5dfc1",
"metadata": {},
"source": [
"JSON (short for _JavaScript Object Notation_) has become one of the standard formats for transmitting data via HTTP request between web browsers and other applications.\n",
"\n",
"JSON is similar to Python code, except for the `null` value and the prohibition of commas at the end of lists. The basic types are objects (dicts), arrays (lists), strings, numbers, Boolean values and `null`. All keys of an object must be strings. There are several Python libraries for reading and writing JSON data. I will use [json](https://docs.python.org/3/library/json.html) from the Python standard library here. To convert a JSON string into Python form, I use `json.loads`:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"id": "59b2dd5f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'Title': 'Python basics',\n",
" 'Language': 'en',\n",
" 'Authors': 'Veit Schiele',\n",
" 'License': 'BSD-3-Clause',\n",
" 'Publication date': '2021-10-28'},\n",
" {'Title': 'Jupyter Tutorial',\n",
" 'Language': 'en',\n",
" 'Authors': 'Veit Schiele',\n",
" 'License': 'BSD-3-Clause',\n",
" 'Publication date': '2019-06-27'}]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"{'Title': 'Python basics', 'Language': 'en', 'Authors': 'Veit Schiele', 'License': 'BSD-3-Clause', 'Publication date': '2021-10-28'}\n",
"{'Title': 'Jupyter Tutorial', 'Language': 'en', 'Authors': 'Veit Schiele', 'License': 'BSD-3-Clause', 'Publication date': '2019-06-27'}\n",
"{'Title': 'Jupyter Tutorial', 'Language': 'de', 'Authors': 'Veit Schiele', 'License': 'BSD-3-Clause', 'Publication date': '2020-10-26'}\n",
"{'Title': 'PyViz Tutorial', 'Language': 'en', 'Authors': 'Veit Schiele', 'License': 'BSD-3-Clause', 'Publication date': '2020-04-13'}\n"
]
}
],
"source": [
"import json\n",
"\n",
"\n",
"results2 = json.loads(books)\n",
"f = open(\"books.json\")\n",
"data = json.load(f)\n",
"\n",
"results2"
"for i in data:\n",
" print(i)"
]
},
{
Expand All @@ -90,23 +50,23 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"id": "ff1d4c89",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'[{\"Title\": \"Python basics\", \"Language\": \"en\", \"Authors\": \"Veit Schiele\", \"License\": \"BSD-3-Clause\", \"Publication date\": \"2021-10-28\"}, {\"Title\": \"Jupyter Tutorial\", \"Language\": \"en\", \"Authors\": \"Veit Schiele\", \"License\": \"BSD-3-Clause\", \"Publication date\": \"2019-06-27\"}]'"
"'[{\"Title\": \"Python basics\", \"Language\": \"en\", \"Authors\": \"Veit Schiele\", \"License\": \"BSD-3-Clause\", \"Publication date\": \"2021-10-28\"}, {\"Title\": \"Jupyter Tutorial\", \"Language\": \"en\", \"Authors\": \"Veit Schiele\", \"License\": \"BSD-3-Clause\", \"Publication date\": \"2019-06-27\"}, {\"Title\": \"Jupyter Tutorial\", \"Language\": \"de\", \"Authors\": \"Veit Schiele\", \"License\": \"BSD-3-Clause\", \"Publication date\": \"2020-10-26\"}, {\"Title\": \"PyViz Tutorial\", \"Language\": \"en\", \"Authors\": \"Veit Schiele\", \"License\": \"BSD-3-Clause\", \"Publication date\": \"2020-04-13\"}]'"
]
},
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"json.dumps(results2)"
"json.dumps(data)"
]
},
{
Expand All @@ -119,7 +79,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"id": "74bc68e8",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -168,17 +128,35 @@
" <td>BSD-3-Clause</td>\n",
" <td>2019-06-27</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Jupyter Tutorial</td>\n",
" <td>de</td>\n",
" <td>Veit Schiele</td>\n",
" <td>BSD-3-Clause</td>\n",
" <td>2020-10-26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>PyViz Tutorial</td>\n",
" <td>en</td>\n",
" <td>Veit Schiele</td>\n",
" <td>BSD-3-Clause</td>\n",
" <td>2020-04-13</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Title Language Authors License Publication date\n",
"0 Python basics en Veit Schiele BSD-3-Clause 2021-10-28\n",
"1 Jupyter Tutorial en Veit Schiele BSD-3-Clause 2019-06-27"
"1 Jupyter Tutorial en Veit Schiele BSD-3-Clause 2019-06-27\n",
"2 Jupyter Tutorial de Veit Schiele BSD-3-Clause 2020-10-26\n",
"3 PyViz Tutorial en Veit Schiele BSD-3-Clause 2020-04-13"
]
},
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -187,7 +165,7 @@
"import pandas as pd\n",
"\n",
"\n",
"df = pd.DataFrame(results2)\n",
"df = pd.DataFrame(data)\n",
"\n",
"df"
]
Expand All @@ -202,7 +180,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"id": "4b6dafe3",
"metadata": {
"scrolled": true
Expand Down Expand Up @@ -281,7 +259,7 @@
"3 PyViz Tutorial en Veit Schiele BSD-3-Clause 2020-04-13"
]
},
"execution_count": 5,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -310,15 +288,15 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"id": "ab209d24",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"Title\":{\"0\":\"Python basics\",\"1\":\"Jupyter Tutorial\"},\"Language\":{\"0\":\"en\",\"1\":\"en\"},\"Authors\":{\"0\":\"Veit Schiele\",\"1\":\"Veit Schiele\"},\"License\":{\"0\":\"BSD-3-Clause\",\"1\":\"BSD-3-Clause\"},\"Publication date\":{\"0\":\"2021-10-28\",\"1\":\"2019-06-27\"}}\n"
"{\"Title\":{\"0\":\"Python basics\",\"1\":\"Jupyter Tutorial\",\"2\":\"Jupyter Tutorial\",\"3\":\"PyViz Tutorial\"},\"Language\":{\"0\":\"en\",\"1\":\"en\",\"2\":\"de\",\"3\":\"en\"},\"Authors\":{\"0\":\"Veit Schiele\",\"1\":\"Veit Schiele\",\"2\":\"Veit Schiele\",\"3\":\"Veit Schiele\"},\"License\":{\"0\":\"BSD-3-Clause\",\"1\":\"BSD-3-Clause\",\"2\":\"BSD-3-Clause\",\"3\":\"BSD-3-Clause\"},\"Publication date\":{\"0\":\"2021-10-28\",\"1\":\"2019-06-27\",\"2\":\"2020-10-26\",\"3\":\"2020-04-13\"}}\n"
]
}
],
Expand All @@ -328,15 +306,15 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"id": "f5dc539c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{\"Title\":\"Python basics\",\"Language\":\"en\",\"Authors\":\"Veit Schiele\",\"License\":\"BSD-3-Clause\",\"Publication date\":\"2021-10-28\"},{\"Title\":\"Jupyter Tutorial\",\"Language\":\"en\",\"Authors\":\"Veit Schiele\",\"License\":\"BSD-3-Clause\",\"Publication date\":\"2019-06-27\"}]\n"
"[{\"Title\":\"Python basics\",\"Language\":\"en\",\"Authors\":\"Veit Schiele\",\"License\":\"BSD-3-Clause\",\"Publication date\":\"2021-10-28\"},{\"Title\":\"Jupyter Tutorial\",\"Language\":\"en\",\"Authors\":\"Veit Schiele\",\"License\":\"BSD-3-Clause\",\"Publication date\":\"2019-06-27\"},{\"Title\":\"Jupyter Tutorial\",\"Language\":\"de\",\"Authors\":\"Veit Schiele\",\"License\":\"BSD-3-Clause\",\"Publication date\":\"2020-10-26\"},{\"Title\":\"PyViz Tutorial\",\"Language\":\"en\",\"Authors\":\"Veit Schiele\",\"License\":\"BSD-3-Clause\",\"Publication date\":\"2020-04-13\"}]\n"
]
}
],
Expand All @@ -361,7 +339,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
"version": "3.11.5"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
Expand Down

0 comments on commit 1998aad

Please sign in to comment.