{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# jsonschema\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`jsonschema` is an implementation of [JSON Schema](https://json-schema.org) for Python (supporting 2.7+ including Python 3)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Usage" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from jsonschema import validate" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A sample schema, like what we'd get from json.load()\n", "schema = {\n", " \"type\" : \"object\",\n", " \"properties\" : {\n", " \"price\" : {\"type\" : \"number\"},\n", " \"name\" : {\"type\" : \"string\"},\n", " },\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# If no exception is raised by validate(), the instance is valid.\n", "validate({\"name\" : \"Eggs\", \"price\" : 34.99}, schema)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "validate(\n", " {\"name\" : \"Eggs\", \"price\" : \"Invalid\"}, schema\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It can also be used from console:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!echo '{\"name\" : \"Eggs\", \"price\" : 34.99}' > /tmp/sample.json" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!echo '{\"type\" : \"object\", \"properties\" : {\"price\" : {\"type\" : \"number\"}, \"name\" : {\"type\" : \"string\"}}}' > /tmp/sample.schema" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!jsonschema -i /tmp/sample.json /tmp/sample.schema" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Do your own experiments here..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try `jsonschema` youself by adding your code below and running your own experiments 👇" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import jsonschema\n", "\n", "# your code here\n", "jsonschema." ] } ], "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }