{ "cells": [ { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:19.036357Z", "start_time": "2019-09-23T18:50:19.031896Z" } }, "source": [ "# JSON Conversions\n", "\n", "This notebook demonstrates how to convert a Resource to JSON and vice-versa. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:20.068658Z", "start_time": "2019-09-23T18:50:19.054054Z" } }, "outputs": [], "source": [ "from kgforge.core import KnowledgeGraphForge" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "forge = KnowledgeGraphForge(\"../../configurations/demo-forge.yml\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Imports" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import json" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from kgforge.core import Resource" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def pp(x): print(json.dumps(x, indent=4))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Resource to JSON" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "address = Resource(type=\"PostalAddress\", country=\"Switzerland\", locality=\"Geneva\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "jane = Resource(type=\"Person\", name=\"Jane Doe\", address=address, email=\"(missing)\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "john = Resource(type=\"Person\", name=\"John Smith\", email=\"john.smith@epfl.ch\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "persons = [jane, john]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 2\n", " _register_one\n", " True\n" ] } ], "source": [ "forge.register(persons)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "association = Resource(type=\"Association\", agent=persons)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " _register_one\n", " True\n" ] } ], "source": [ "forge.register(association)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"id\": \"e5ed50d2-bc03-45c5-9953-4efed4ddfb0d\",\n", " \"type\": \"Association\",\n", " \"agent\": [\n", " {\n", " \"id\": \"983b316e-db52-4308-8c7b-979654cab3c8\",\n", " \"type\": \"Person\",\n", " \"address\": {\n", " \"type\": \"PostalAddress\",\n", " \"country\": \"Switzerland\",\n", " \"locality\": \"Geneva\"\n", " },\n", " \"email\": \"(missing)\",\n", " \"name\": \"Jane Doe\"\n", " },\n", " {\n", " \"id\": \"8dd74b04-b9b5-4ad2-a9e0-63251e514ebe\",\n", " \"type\": \"Person\",\n", " \"email\": \"john.smith@epfl.ch\",\n", " \"name\": \"John Smith\"\n", " }\n", " ]\n", "}\n" ] } ], "source": [ "pp(forge.as_json(association))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"id\": \"983b316e-db52-4308-8c7b-979654cab3c8\",\n", " \"type\": \"Person\",\n", " \"address\": {\n", " \"type\": \"PostalAddress\",\n", " \"country\": \"Switzerland\",\n", " \"locality\": \"Geneva\"\n", " },\n", " \"email\": \"(missing)\",\n", " \"name\": \"Jane Doe\",\n", " \"deprecated\": false,\n", " \"version\": 1\n", "}\n" ] } ], "source": [ "pp(forge.as_json(jane, store_metadata=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## JSON to Resource" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "address = Resource(type=\"PostalAddress\", country=\"Switzerland\", locality=\"Geneva\")" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "jane = Resource(type=\"Person\", name=\"Jane Doe\", address=address, email=\"(missing)\")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "john = Resource(type=\"Person\", name=\"John Smith\", email=\"john.smith@epfl.ch\")" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "persons = [jane, john]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "association = Resource(type=\"Association\", agent=[jane, john])" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "data = {\n", " \"type\": \"Association\",\n", " \"agent\": [\n", " {\n", " \"type\": \"Person\",\n", " \"address\": {\n", " \"type\": \"PostalAddress\",\n", " \"country\": \"Switzerland\",\n", " \"locality\": \"Geneva\",\n", " },\n", " \"email\": \"(missing)\",\n", " \"name\": \"Jane Doe\"\n", " },\n", " {\n", " \"type\": \"Person\",\n", " \"email\": \"john.smith@epfl.ch\",\n", " \"name\": \"John Smith\"\n", " }\n", " ]\n", "}" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "resource = forge.from_json(data)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "resource == association" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " type: Association\n", " agent:\n", " [\n", " {\n", " type: Person\n", " address:\n", " {\n", " type: PostalAddress\n", " country: Switzerland\n", " locality: Geneva\n", " }\n", " name: Jane Doe\n", " }\n", " {\n", " type: Person\n", " email: john.smith@epfl.ch\n", " name: John Smith\n", " }\n", " ]\n", "}\n" ] } ], "source": [ "print(forge.from_json(data, na=\"(missing)\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "kgforge(v2)", "language": "python", "name": "kgforge" }, "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.7.5" } }, "nbformat": 4, "nbformat_minor": 4 }