{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e102548e-834b-4e03-9674-4f21910610b4",
   "metadata": {},
   "source": [
    "# JSON-LD\n",
    "\n",
    "minimales Beispiel mit `rdflib`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c1de3b9-0bf9-4731-9a19-5d869e458adb",
   "metadata": {},
   "source": [
    "## ohne rdflib"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d61c9825-b124-46e9-8902-2de71156d70c",
   "metadata": {},
   "source": [
    "Motivation: Ein Datensatz, bestehend aus (einer Liste von) zwei Records mit anonymen IDs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3bf90d46-159b-499e-a88d-0b55690e6d5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "json_string_ohne_ld = \"\"\"[\n",
    "  {\n",
    "    \"subject\": \"https://orcid.org/0000-0002-7051-6198\",\n",
    "    \"name\": \"Johannes Busse\",\n",
    "    \"homepage\": \"http://jbusse.de\"\n",
    "  },\n",
    "  {\n",
    "    \"subject\": \"https://orcid.org/0000-0001-2345-6789\",\n",
    "    \"names\": [\n",
    "      \"Maria Mustermann\",\n",
    "      \"M. Mustermann\"\n",
    "    ],\n",
    "    \"homepage\": \"https://example.org\"\n",
    "  }\n",
    "]\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "6dfcd641-7afa-4d20-8991-4b2c9afe49f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'subject': 'https://orcid.org/0000-0002-7051-6198',\n",
       "  'name': 'Johannes Busse',\n",
       "  'homepage': 'http://jbusse.de'},\n",
       " {'subject': 'https://orcid.org/0000-0001-2345-6789',\n",
       "  'names': ['Maria Mustermann', 'M. Mustermann'],\n",
       "  'homepage': 'https://example.org'}]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json\n",
    "daten = json.loads(json_string_ohne_ld)\n",
    "daten"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22d7cbad-7b3c-4015-add2-223cc22d27e9",
   "metadata": {},
   "source": [
    "Eigentlich will man aber so etwas haben:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "9dc8138e-a194-489b-b587-dd6f3f171739",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'https://orcid.org/0000-0002-7051-6198': {'name': 'Johannes Busse',\n",
       "  'homepage': 'http://jbusse.de'},\n",
       " 'https://orcid.org/0000-0001-2345-6789': {'names': ['Maria Mustermann',\n",
       "   'M. Mustermann'],\n",
       "  'homepage': 'https://example.org'}}"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "daten_gewuenscht = { \n",
    "    record['subject']: { k: v for k,v in record.items() if not k == 'subject'}\n",
    "    for record in daten }\n",
    "daten_gewuenscht"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c0116db-cd78-4d8c-9aa5-a4063d0d9f7b",
   "metadata": {},
   "source": [
    "## mit rdflib\n",
    "\n",
    "Motivation: Von 3-Star mach 4-Star mit minimalem Aufwand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bc6b0e29-eb62-42c1-b66a-69e159cae9b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import rdflib"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b87ef760-8e5d-498b-a227-2552e238580e",
   "metadata": {},
   "source": [
    "Wir vereinfachen und fokussieren auf den ersten Datensatz. In JSON-LD ist `@id` ein Schlüsselwort, definiert das Subject eines Tripels."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "ca1acb25-2bf9-4792-a82a-46f39981db62",
   "metadata": {},
   "outputs": [],
   "source": [
    "jsonldstring1 = \"\"\"\n",
    "{\n",
    "  \"@context\": {\n",
    "    \"name\": \"http://xmlns.com/foaf/0.1/name\",\n",
    "    \"homepage\": \"http://xmlns.com/foaf/0.1/homepage\"\n",
    "  },\n",
    "  \"@id\": \"https://orcid.org/0000-0002-7051-6198\",\n",
    "  \"name\": \"Johannes Busse\",\n",
    "  \"homepage\": \"http://jbusse.de/\"\n",
    "}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "5ad60ef8-3234-437e-8579-9341c4dc5fa5",
   "metadata": {},
   "outputs": [],
   "source": [
    "#g1_dict = json.loads(jsonldstring1)\n",
    "#g1_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "dceceadf-ae07-4f36-94de-d33bf3a32949",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n",
      "\n",
      "<https://orcid.org/0000-0002-7051-6198> foaf:homepage \"http://jbusse.de/\" ;\n",
      "    foaf:name \"Johannes Busse\" .\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "g1 = rdflib.Graph().parse( data=jsonldstring1, format=\"json-ld\" )\n",
    "print( g1.serialize() )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21836fec-2b65-4cc1-804e-3e6c353ab804",
   "metadata": {},
   "source": [
    "noch kompakter: Präfixe in Strings werden aufgelöst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "352eb6e5-711a-4097-a993-4c804658a61e",
   "metadata": {},
   "outputs": [],
   "source": [
    "jsonldstring2 = \"\"\"\n",
    "{\n",
    "  \"@context\": {\n",
    "    \"name\": \"http://xmlns.com/foaf/0.1/name\",\n",
    "    \"homepage\": \"http://xmlns.com/foaf/0.1/homepage\",\n",
    "    \"orcid\": \"https://orcid.org/\"\n",
    "  },\n",
    "  \"@id\": \"orcid:0000-0002-7051-6198\",\n",
    "  \"name\": \"Johannes Busse\",\n",
    "  \"homepage\": \"http://jbusse.de/\"\n",
    "}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "2a189856-ac02-4cd5-b499-d48751f8a0f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n",
      "@prefix orcid: <https://orcid.org/> .\n",
      "\n",
      "orcid:0000-0002-7051-6198 foaf:homepage \"http://jbusse.de/\" ;\n",
      "    foaf:name \"Johannes Busse\" .\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "g2 = rdflib.Graph().parse( data=jsonldstring2, format=\"json-ld\" )\n",
    "print( g2.serialize() )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "145dbfea-0608-45f0-afb8-6d70025710e2",
   "metadata": {},
   "source": [
    "Mit Keyword-Aliasing: mappe `subject` auf `@id`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "52d62c27-9d81-4ace-8f8a-874dd70a69e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "jsonldstring3 = \"\"\"\n",
    "{\n",
    "  \"@context\": {\n",
    "    \"name\": \"http://xmlns.com/foaf/0.1/name\",\n",
    "    \"homepage\": \"http://xmlns.com/foaf/0.1/homepage\",\n",
    "    \"orcid\": \"https://orcid.org/\",\n",
    "    \"subject\": \"@id\"\n",
    "  },\n",
    "  \"subject\": \"orcid:0000-0002-7051-6198\",\n",
    "  \"name\": \"Johannes Busse\",\n",
    "  \"homepage\": \"http://jbusse.de/\"\n",
    "}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "2c683c51-1824-450b-9fb1-6062bc0b9d9c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n",
      "@prefix orcid: <https://orcid.org/> .\n",
      "\n",
      "orcid:0000-0002-7051-6198 foaf:homepage \"http://jbusse.de/\" ;\n",
      "    foaf:name \"Johannes Busse\" .\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "g3 = rdflib.Graph().parse( data=jsonldstring3, format=\"json-ld\" )\n",
    "print( g3.serialize() )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "a0c0aa95-bf49-4f41-bd47-496c3d257a0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "json_string_mit_ld = \"\"\"{\n",
    "  \"@context\": {\n",
    "    \"name\": \"http://xmlns.com/foaf/0.1/name\",\n",
    "    \"homepage\": \"http://xmlns.com/foaf/0.1/homepage\",\n",
    "    \"orcid\": \"https://orcid.org/\",\n",
    "    \"subject\": \"@id\"\n",
    "  },\n",
    "  \"@graph\": [\n",
    "  \n",
    "  {\n",
    "    \"subject\": \"https://orcid.org/0000-0002-7051-6198\",\n",
    "    \"name\": \"Johannes Busse\",\n",
    "    \"homepage\": \"http://jbusse.de\"\n",
    "  },\n",
    "  {\n",
    "    \"subject\": \"https://orcid.org/0000-0001-2345-6789\",\n",
    "    \"name\": [\n",
    "      \"Maria Mustermann\",\n",
    "      \"M. Mustermann\"\n",
    "    ],\n",
    "    \"homepage\": \"https://example.org\"\n",
    "  }\n",
    "  ]\n",
    "}\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "ae5e436a-870a-42ca-b061-aba583f55a4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n",
      "@prefix orcid: <https://orcid.org/> .\n",
      "\n",
      "orcid:0000-0001-2345-6789 foaf:homepage \"https://example.org\" ;\n",
      "    foaf:name \"M. Mustermann\",\n",
      "        \"Maria Mustermann\" .\n",
      "\n",
      "orcid:0000-0002-7051-6198 foaf:homepage \"http://jbusse.de\" ;\n",
      "    foaf:name \"Johannes Busse\" .\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "g4 = rdflib.Graph().parse( data=json_string_mit_ld, format=\"json-ld\" )\n",
    "print( g4.serialize() )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df13989e-86b3-42ac-a5ae-31f715b06a03",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae073098-3edd-412d-87f1-1a8d86c2aeb3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
