{ "cells": [ { "cell_type": "markdown", "id": "71f24608", "metadata": {}, "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.\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": 1, "id": "59b2dd5f", "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'}\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", "with open(\"books.json\") as f:\n", " data = json.load(f)\n", "\n", " for i in data:\n", " print(i)" ] }, { "cell_type": "markdown", "id": "0eabc1ba", "metadata": {}, "source": [ "[json.dumps](https://docs.python.org/3/library/json.html#json.dumps), on the other hand, converts a Python object back to JSON:" ] }, { "cell_type": "code", "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\": \"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": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "json.dumps(data)" ] }, { "cell_type": "markdown", "id": "a61f1769", "metadata": {}, "source": [ "How you convert a JSON object or list of objects into a DataFrame or other data structure for analysis is up to you. Conveniently, you can pass a list of dicts (which were previously JSON objects) to the DataFrame constructor:" ] }, { "cell_type": "code", "execution_count": 3, "id": "74bc68e8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TitleLanguageAuthorsLicensePublication date
0Python basicsenVeit SchieleBSD-3-Clause2021-10-28
1Jupyter TutorialenVeit SchieleBSD-3-Clause2019-06-27
2Jupyter TutorialdeVeit SchieleBSD-3-Clause2020-10-26
3PyViz TutorialenVeit SchieleBSD-3-Clause2020-04-13
\n", "
" ], "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\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": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "\n", "df = pd.DataFrame(data)\n", "\n", "df" ] }, { "cell_type": "markdown", "id": "fe3deacb", "metadata": {}, "source": [ "[pandas.read_json](https://pandas.pydata.org/docs/reference/api/pandas.read_json.html) can automatically convert JSON records in certain arrangements into a Series or DataFrame, for example:" ] }, { "cell_type": "code", "execution_count": 4, "id": "4b6dafe3", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TitleLanguageAuthorsLicensePublication date
0Python basicsenVeit SchieleBSD-3-Clause2021-10-28
1Jupyter TutorialenVeit SchieleBSD-3-Clause2019-06-27
2Jupyter TutorialdeVeit SchieleBSD-3-Clause2020-10-26
3PyViz TutorialenVeit SchieleBSD-3-Clause2020-04-13
\n", "
" ], "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\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, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df2 = pd.read_json(\"books.json\")\n", "\n", "df2" ] }, { "cell_type": "markdown", "id": "a5d22012", "metadata": {}, "source": [ "The default options for `pandas.read_json` assume that each object in the JSON array is a row in the table." ] }, { "cell_type": "markdown", "id": "e8730269", "metadata": {}, "source": [ "If you want to export data from pandas to JSON, you can use [pandas.DataFrame.to_json](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_json.html):" ] }, { "cell_type": "code", "execution_count": 5, "id": "ab209d24", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"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" ] } ], "source": [ "print(df.to_json())" ] }, { "cell_type": "code", "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\"},{\"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" ] } ], "source": [ "print(df.to_json(orient=\"records\"))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "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.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }