{ "cells": [ { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:19.036357Z", "start_time": "2019-09-23T18:50:19.031896Z" } }, "source": [ "# JSON-LD Conversions\n", "\n", "This notebook demonstrates how to [convert](https://nexus-forge.readthedocs.io/en/latest/interaction.html#converting) Resources to [JSON-LD](https://json-ld.org) and vice-versa. JSON-LD is a semantic-preserving JSON format allowing to provide identifiers and meaning to JSON keys and values by mean of an added '@context' object. Read the excellent [JSON-LD documentation](https://json-ld.org/learn.html) to learn more about this format.\n", "\n", "A JSON-LD context can be assigned to a [Resource](https://nexus-forge.readthedocs.io/en/latest/interaction.html#resource) in three ways by order of priority:\n", "1. directly set in the resource using the property `context`: e.g.`jane_resource.context=\"https://schema.org/docs/jsonldcontext.json\"`\n", "2. set in the `Model` section of the forge configuration file: e.g.\n", "\n", "\n", "'''\n", "\n", "Model:\n", "\n", " name: RdfModel\n", " \n", " origin: ...\n", " \n", " source: ...\n", " \n", " context:\n", " \n", " iri: \"https://schema.org/docs/jsonldcontext.json\"\n", " \n", "''' \n", "\n", "\n", "3. set by the configured store. A store (such as BlueBrainNexus) can support JSON-LD and add a default JSON-LD context whenever one is not provided." ] }, { "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): \n", " print(json.dumps(x, indent=4))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Context" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "context = {\n", " \"ex\": \"http://example.org/\",\n", " \"Person\": \"ex:Person\",\n", " \"Organization\": \"ex:Organization\",\n", " \"employer\": \"ex:employer\",\n", " \"name\": \"ex:name\",\n", " \"award\":\"ex:award\",\n", " \"email\":\"ex:email\"\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Resource to JSON-LD" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### context from the user" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### locally defined context" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "jane = Resource(context=context, type=\"Person\", name=\"Jane Doe\", award=[\"Nobel\"], email=[\"jane.doe1@example.org\", \"jane.doe2@example.org\"])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'type': 'Person',\n", " 'award': ['Nobel'],\n", " 'email': ['jane.doe1@example.org', 'jane.doe2@example.org'],\n", " 'name': 'Jane Doe'}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "forge.as_json(jane)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"@context\": {\n", " \"ex\": \"http://example.org/\",\n", " \"Person\": \"ex:Person\",\n", " \"Organization\": \"ex:Organization\",\n", " \"employer\": \"ex:employer\",\n", " \"name\": \"ex:name\",\n", " \"award\": \"ex:award\",\n", " \"email\": \"ex:email\"\n", " },\n", " \"@type\": \"Person\",\n", " \"name\": \"Jane Doe\",\n", " \"award\": [\n", " \"Nobel\"\n", " ],\n", " \"email\": [\n", " \"jane.doe1@example.org\",\n", " \"jane.doe2@example.org\"\n", " ]\n", "}\n" ] } ], "source": [ "pp(forge.as_jsonld(jane)) # default JSON-LD form==\"compacted\"" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"@type\": [\n", " \"http://example.org/Person\"\n", " ],\n", " \"http://example.org/award\": [\n", " {\n", " \"@value\": \"Nobel\"\n", " }\n", " ],\n", " \"http://example.org/email\": [\n", " {\n", " \"@value\": \"jane.doe1@example.org\"\n", " },\n", " {\n", " \"@value\": \"jane.doe2@example.org\"\n", " }\n", " ],\n", " \"http://example.org/name\": [\n", " {\n", " \"@value\": \"Jane Doe\"\n", " }\n", " ]\n", "}\n" ] } ], "source": [ "pp(forge.as_jsonld(jane, form=\"expanded\"))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"@context\": {\n", " \"ex\": \"http://example.org/\",\n", " \"Person\": \"ex:Person\",\n", " \"Organization\": \"ex:Organization\",\n", " \"employer\": \"ex:employer\",\n", " \"name\": \"ex:name\",\n", " \"award\": \"ex:award\",\n", " \"email\": \"ex:email\"\n", " },\n", " \"@type\": \"Person\",\n", " \"name\": \"Jane Doe\",\n", " \"award\": [\n", " \"Nobel\"\n", " ],\n", " \"email\": [\n", " \"jane.doe1@example.org\",\n", " \"jane.doe2@example.org\"\n", " ]\n", "}\n" ] } ], "source": [ "pp(forge.as_jsonld(jane, form=\"compacted\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### remote context from the web" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "jane = Resource(context=\"https://schema.org/docs/jsonldcontext.json\", type=\"Person\", name=\"Jane Doe\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " context: https://schema.org/docs/jsonldcontext.json\n", " type: Person\n", " name: Jane Doe\n", "}\n" ] } ], "source": [ "print(jane)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"@type\": [\n", " \"http://schema.org/Person\"\n", " ],\n", " \"http://schema.org/name\": [\n", " {\n", " \"@value\": \"Jane Doe\"\n", " }\n", " ]\n", "}\n" ] } ], "source": [ "pp(forge.as_jsonld(jane, form=\"expanded\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### context from the model\n", "\n", "The configured model provides a default context that will be used to create resources that do not have context provided." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "jane = Resource(type=\"Person\", name=\"Jane Doe\")" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " type: Person\n", " name: Jane Doe\n", "}\n" ] } ], "source": [ "print(jane)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"@context\": \"https://bbp.neuroshapes.org\",\n", " \"@type\": \"Person\",\n", " \"name\": \"Jane Doe\"\n", "}\n" ] } ], "source": [ "pp(forge.as_jsonld(jane))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"@type\": [\n", " \"http://schema.org/Person\"\n", " ],\n", " \"http://schema.org/name\": [\n", " {\n", " \"@value\": \"Jane Doe\"\n", " }\n", " ]\n", "}\n" ] } ], "source": [ "pp(forge.as_jsonld(jane, form=\"expanded\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### context from the Store\n", "\n", "It is possible to use a context that is available in the configured RdfModel Store" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## JSON-LD to Resource" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "john = Resource(context=context, type=\"Person\", name=\"John Smith\", award=[\"A.M. TURING AWARD\"], \n", " email=[\"john.smith1@example.org\", \"john.smith2@example.org\"])" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "data = {\n", " \"@context\": context,\n", " \"@type\": \"Person\",\n", " \"name\": \"John Smith\",\n", " \"award\":[\"A.M. TURING AWARD\"],\n", " \"email\":[\"john.smith1@example.org\", \"john.smith2@example.org\"]\n", "}" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "resource = forge.from_jsonld(data)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "resource == john" ] } ], "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 }