{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2023
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 9.5 Serialization with JSON " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### JSON Data Format " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The **JSON** (JavaScript Object Notation) data format is very commonly used by web applications and cloud-based services such as Twitter.\n", "#### A JSON object is similar to a Python dictionary. Each object contains a comma-separated list of _property names_ and _values_ surrounded by curly braces. For example:\n", "```\n", "{'account': 100, 'name': 'Jones', 'balance': 24.98}\n", "```\n", "#### JSON also has arrays, which look like Python lists. For example:\n", "```\n", "[100, 200, 300]\n", "```\n", "#### Values in JSON objects and arrays can be:\n", "- strings in _double_ quotes\n", "- numbers\n", "- Boolean values `true` and `false`\n", "- `null` (no value, like Python's `None`)\n", "- arrays\n", "- other (nested) JSON objects\n", "#### Python supports JSON through its Standard Library module `json`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts_dict = {'accounts': [ {'account': 100, 'name': 'Jones', 'balance': 24.98},\n", " {'account': 200, 'name': 'Doe', 'balance': 345.67}\n", " ]\n", " }\n", "\n", "accounts_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Serializing an Object to JSON\n", "#### We can **serialize** (convert to a stream of bytes) a dictionary object to a text file in JSON format with the `json` module's `dump()` function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "\n", "with open('accounts.json', 'w') as accounts:\n", " json.dump(accounts_dict, accounts)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Deserializing the JSON Text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Conversely, the `json` module's `load()` function reads a text file in JSON format and **deserializes** the data into a Python dictionary object." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with open('accounts.json', 'r') as accounts:\n", " accounts_json = json.load(accounts)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts_json" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts_json['accounts']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts_json['accounts'][0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts_json['accounts'][1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Displaying the JSON Text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The `json` module's `dumps()` function (for \"dump string\") _pretty-prints_ (prints in a nice format) a JSON object." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with open('accounts.json', 'r') as accounts:\n", " print(json.dumps(json.load(accounts), indent=4))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness. The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (C) Copyright 2023 by Ronald Mak" ] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 4 }