-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<!-- \n", | ||
" # Copyright (C) 2023-2024 Y Hsu <[email protected]>\n", | ||
" #\n", | ||
" # This program is free software: you can redistribute it and/or modify\n", | ||
" # it under the terms of the GNU General Public license as published by\n", | ||
" # the Free software Foundation, either version 3 of the License, or\n", | ||
" # any later version.\n", | ||
" #\n", | ||
" # This program is distributed in the hope that it will be useful,\n", | ||
" # but WITHOUT ANY WARRANTY; without even the implied warranty of\n", | ||
" # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n", | ||
" # GNU General Public License for more details\n", | ||
" #\n", | ||
" # You should have received a copy of the GNU General Public license\n", | ||
" # along with this program. If not, see <https://www.gnu.org/license/> \n", | ||
"-->" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# U/Data/JSON tools\n", | ||
"\n", | ||
"JavaScript Object Notation (JSON) is a data exchange format deigned to be \"minimal, portable, textual, and a subset of JavaScript\"[^1].\n", | ||
"\n", | ||
"The module `util.jsonutil` depends on package `json` [^1] and provide simplified interface for creating keys summary, extracting values by keys, and preview. \n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## JSON Format Refresh\n", | ||
"\n", | ||
"JSON grammar, data structure, and conformance rules can be found in reference [^3]. \n", | ||
"\n", | ||
"Below is an example of using `json` to process a JSON object found in reference [^4]:\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "plaintext" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import json \n", | ||
"\n", | ||
"ex1 = {\n", | ||
" \"Image\": {\n", | ||
" \"Width\": 800,\n", | ||
" \"Height\": 600,\n", | ||
" \"Title\": \"View from 15th Floor\",\n", | ||
" \"Thumbnail\": {\n", | ||
" \"Url\": \"http://na/image/481989943\",\n", | ||
" \"Height\": 125,\n", | ||
" \"Width\": 100\n", | ||
" },\n", | ||
" \"Animated\" : False,\n", | ||
" \"IDs\": [116, 943, 234, 38793]\n", | ||
" }\n", | ||
"}\n", | ||
"print(json.dumps(ex1, indent=2))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## JSON Schema" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "plaintext" | ||
} | ||
}, | ||
"source": [ | ||
"For a larger dataset, a foundation schema or structure template is helpful to infer, create, modify and validate JSON objects to ensure correct data exchange.\n", | ||
"A brief history and links about JSON schema can be found in reference [^5].\n", | ||
"A schema object describe the structure of elements within a JSON data object [^6].\n", | ||
"\n", | ||
"Please note that there are multiple Python tools available for generating JSON schema. \n", | ||
"\n", | ||
"To generate a schema from an object using package `genson`:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "plaintext" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from genson import SchemaBuilder\n", | ||
"\n", | ||
"builder = SchemaBuilder()\n", | ||
"builder.add_object(ex1['Image'])\n", | ||
"builder.to_schema()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"To generate a schema from a list of objects (records) using package `genson`: " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "plaintext" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"ex2 = [ex1['Image']] + [\n", | ||
" {\n", | ||
" \"Width\": \"701\",\n", | ||
" \"Height\": False,\n", | ||
" \"Title\": \"View from 16th Floor\",\n", | ||
" }\n", | ||
"]\n", | ||
"\n", | ||
"builder = SchemaBuilder()\n", | ||
"builder.add_object(ex2)\n", | ||
"print(json.dumps(builder.to_schema(), indent=2))\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The output of `to_schema()` is a dictionary type object and can be edited as needed.\n", | ||
"The package `genson` also provides functions to update `builder`, and to create extended schema builder." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Reference\n", | ||
"\n", | ||
"[^1]: IETF. (2014). RFC 7159: The JavaScript Object Notation (JSON) Data Interchange Format. ([web page])https://datatracker.ietf.org/doc/html/rfc7159.html#section-1))\n", | ||
"[^2]: Python Software Foundation. (2024). json — JSON encoder and decoder. ([web page](https://docs.python.org/3/library/json.html#json-to-py-table))\n", | ||
"[^3]: IETF. (2014). RFC 7159: The JavaScript Object Notation (JSON) Data Interchange Format. ([web page])https://datatracker.ietf.org/doc/html/rfc7159.html#section-2))\n", | ||
"[^4]: IETF. (2014). RFC 7159: The JavaScript Object Notation (JSON) Data Interchange Format. ([web page])https://datatracker.ietf.org/doc/html/rfc7159.html#section-13))\n", | ||
"[^5]: The JSON Schema Organization. (year). History of JSON Schema. ([web page](https://json-schema.org/overview/what-is-jsonschema#history-of-json-schema))\n", | ||
"[^6]: The JSON Schema Organization. (year). Creating your first schema. ([web page](https://json-schema.org/learn/getting-started-step-by-step))\n", | ||
"[&7]: wolverdude. (2024). genson 1.3.0. ([web page](https://pypi.org/project/genson/)) | ([github](https://github.com/wolverdude/genson/))" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"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.12.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters