{ "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](https://nexus-forge.readthedocs.io/en/latest/interaction.html#converting) a [Resource](https://nexus-forge.readthedocs.io/en/latest/interaction.html#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": "markdown", "metadata": {}, "source": [ "A configuration file is needed in order to create a KnowledgeGraphForge session. A configuration can be generated using the notebook [00-Initialization.ipynb](00%20-%20Initialization.ipynb)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "forge = KnowledgeGraphForge(\"../../configurations/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)" ] }, { "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_many\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\": \"https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/101a874a-0e59-404f-aa89-4e9fc9119520\",\n", " \"type\": \"Association\",\n", " \"agent\": [\n", " {\n", " \"id\": \"https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/caccdb9d-a374-4ab4-85b1-01efb1d8d5e9\",\n", " \"type\": \"Person\",\n", " \"address\": {\n", " \"type\": \"PostalAddress\",\n", " \"country\": \"Switzerland\",\n", " \"locality\": \"Geneva\"\n", " },\n", " \"name\": \"Jane Doe\"\n", " },\n", " {\n", " \"id\": \"https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/e3817898-d82c-442c-a53e-6367fde62beb\",\n", " \"type\": \"Person\",\n", " \"email\": \"john.smith@epfl.ch\",\n", " \"name\": \"John Smith\"\n", " }\n", " ],\n", " \"_constrainedBy\": \"https://bluebrain.github.io/nexus/schemas/unconstrained.json\",\n", " \"_createdAt\": \"2022-04-12T22:22:23.988Z\",\n", " \"_createdBy\": \"https://bbp.epfl.ch/nexus/v1/realms/bbp/users/sy\",\n", " \"_deprecated\": false,\n", " \"_incoming\": \"https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/101a874a-0e59-404f-aa89-4e9fc9119520/incoming\",\n", " \"_outgoing\": \"https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/101a874a-0e59-404f-aa89-4e9fc9119520/outgoing\",\n", " \"_project\": \"https://bbp.epfl.ch/nexus/v1/projects/dke/kgforge\",\n", " \"_rev\": 1,\n", " \"_schemaProject\": \"https://bbp.epfl.ch/nexus/v1/projects/dke/kgforge\",\n", " \"_self\": \"https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/101a874a-0e59-404f-aa89-4e9fc9119520\",\n", " \"_updatedAt\": \"2022-04-12T22:22:23.988Z\",\n", " \"_updatedBy\": \"https://bbp.epfl.ch/nexus/v1/realms/bbp/users/sy\"\n", "}\n" ] } ], "source": [ "pp(forge.as_json(association, store_metadata=True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## JSON to Resource" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "address = Resource(type=\"PostalAddress\", country=\"Switzerland\", locality=\"Geneva\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "jane = Resource(type=\"Person\", name=\"Jane Doe\", address=address, award=[\"Nobel\"])" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "john = Resource(type=\"Person\", name=\"John Smith\", email=\"john.smith@epfl.ch\")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "persons = [jane, john]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "association = Resource(type=\"Association\", agent=[jane, john])" ] }, { "cell_type": "code", "execution_count": 19, "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", " \"award\":[\"Nobel\"]\n", " },\n", " {\n", " \"type\": \"Person\",\n", " \"email\": \"john.smith@epfl.ch\",\n", " \"name\": \"John Smith\"\n", " }\n", " ]\n", "}" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "resource = forge.from_json(data, na=\"(missing)\")" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "resource == association" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.7 (nexusforgelatest)", "language": "python", "name": "nexusforgelatest" }, "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.10" } }, "nbformat": 4, "nbformat_minor": 4 }