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 @@ "