diff --git a/docs/data-processing/serialisation-formats/json/example.ipynb b/docs/data-processing/serialisation-formats/json/example.ipynb index 32ce9d84..f1c1e368 100644 --- a/docs/data-processing/serialisation-formats/json/example.ipynb +++ b/docs/data-processing/serialisation-formats/json/example.ipynb @@ -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)" ] }, { @@ -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)" ] }, { @@ -119,7 +79,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "id": "74bc68e8", "metadata": {}, "outputs": [ @@ -168,6 +128,22 @@ " BSD-3-Clause\n", " 2019-06-27\n", " \n", + " \n", + " 2\n", + " Jupyter Tutorial\n", + " de\n", + " Veit Schiele\n", + " BSD-3-Clause\n", + " 2020-10-26\n", + " \n", + " \n", + " 3\n", + " PyViz Tutorial\n", + " en\n", + " Veit Schiele\n", + " BSD-3-Clause\n", + " 2020-04-13\n", + " \n", " \n", "\n", "" @@ -175,10 +151,12 @@ "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" } @@ -187,7 +165,7 @@ "import pandas as pd\n", "\n", "\n", - "df = pd.DataFrame(results2)\n", + "df = pd.DataFrame(data)\n", "\n", "df" ] @@ -202,7 +180,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "4b6dafe3", "metadata": { "scrolled": true @@ -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" } @@ -310,7 +288,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "id": "ab209d24", "metadata": {}, "outputs": [ @@ -318,7 +296,7 @@ "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" ] } ], @@ -328,7 +306,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "id": "f5dc539c", "metadata": {}, "outputs": [ @@ -336,7 +314,7 @@ "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" ] } ], @@ -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": {