JSON-LD

JSON-LD#

minimales Beispiel mit rdflib

ohne rdflib#

Motivation: Ein Datensatz, bestehend aus (einer Liste von) zwei Records mit anonymen IDs:

json_string_ohne_ld = """[
  {
    "subject": "https://orcid.org/0000-0002-7051-6198",
    "name": "Johannes Busse",
    "homepage": "http://jbusse.de"
  },
  {
    "subject": "https://orcid.org/0000-0001-2345-6789",
    "names": [
      "Maria Mustermann",
      "M. Mustermann"
    ],
    "homepage": "https://example.org"
  }
]
"""
import json
daten = json.loads(json_string_ohne_ld)
daten
[{'subject': 'https://orcid.org/0000-0002-7051-6198',
  'name': 'Johannes Busse',
  'homepage': 'http://jbusse.de'},
 {'subject': 'https://orcid.org/0000-0001-2345-6789',
  'names': ['Maria Mustermann', 'M. Mustermann'],
  'homepage': 'https://example.org'}]

Eigentlich will man aber so etwas haben:

daten_gewuenscht = { 
    record['subject']: { k: v for k,v in record.items() if not k == 'subject'}
    for record in daten }
daten_gewuenscht
{'https://orcid.org/0000-0002-7051-6198': {'name': 'Johannes Busse',
  'homepage': 'http://jbusse.de'},
 'https://orcid.org/0000-0001-2345-6789': {'names': ['Maria Mustermann',
   'M. Mustermann'],
  'homepage': 'https://example.org'}}

mit rdflib#

Motivation: Von 3-Star mach 4-Star mit minimalem Aufwand

import rdflib

Wir vereinfachen und fokussieren auf den ersten Datensatz. In JSON-LD ist @id ein Schlüsselwort, definiert das Subject eines Tripels.

jsonldstring1 = """
{
  "@context": {
    "name": "http://xmlns.com/foaf/0.1/name",
    "homepage": "http://xmlns.com/foaf/0.1/homepage"
  },
  "@id": "https://orcid.org/0000-0002-7051-6198",
  "name": "Johannes Busse",
  "homepage": "http://jbusse.de/"
}
"""
#g1_dict = json.loads(jsonldstring1)
#g1_dict
g1 = rdflib.Graph().parse( data=jsonldstring1, format="json-ld" )
print( g1.serialize() )
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<https://orcid.org/0000-0002-7051-6198> foaf:homepage "http://jbusse.de/" ;
    foaf:name "Johannes Busse" .

noch kompakter: Präfixe in Strings werden aufgelöst

jsonldstring2 = """
{
  "@context": {
    "name": "http://xmlns.com/foaf/0.1/name",
    "homepage": "http://xmlns.com/foaf/0.1/homepage",
    "orcid": "https://orcid.org/"
  },
  "@id": "orcid:0000-0002-7051-6198",
  "name": "Johannes Busse",
  "homepage": "http://jbusse.de/"
}
"""
g2 = rdflib.Graph().parse( data=jsonldstring2, format="json-ld" )
print( g2.serialize() )
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix orcid: <https://orcid.org/> .

orcid:0000-0002-7051-6198 foaf:homepage "http://jbusse.de/" ;
    foaf:name "Johannes Busse" .

Mit Keyword-Aliasing: mappe subject auf @id.

jsonldstring3 = """
{
  "@context": {
    "name": "http://xmlns.com/foaf/0.1/name",
    "homepage": "http://xmlns.com/foaf/0.1/homepage",
    "orcid": "https://orcid.org/",
    "subject": "@id"
  },
  "subject": "orcid:0000-0002-7051-6198",
  "name": "Johannes Busse",
  "homepage": "http://jbusse.de/"
}
"""
g3 = rdflib.Graph().parse( data=jsonldstring3, format="json-ld" )
print( g3.serialize() )
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix orcid: <https://orcid.org/> .

orcid:0000-0002-7051-6198 foaf:homepage "http://jbusse.de/" ;
    foaf:name "Johannes Busse" .
json_string_mit_ld = """{
  "@context": {
    "name": "http://xmlns.com/foaf/0.1/name",
    "homepage": "http://xmlns.com/foaf/0.1/homepage",
    "orcid": "https://orcid.org/",
    "subject": "@id"
  },
  "@graph": [
  
  {
    "subject": "https://orcid.org/0000-0002-7051-6198",
    "name": "Johannes Busse",
    "homepage": "http://jbusse.de"
  },
  {
    "subject": "https://orcid.org/0000-0001-2345-6789",
    "name": [
      "Maria Mustermann",
      "M. Mustermann"
    ],
    "homepage": "https://example.org"
  }
  ]
}
"""
g4 = rdflib.Graph().parse( data=json_string_mit_ld, format="json-ld" )
print( g4.serialize() )
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix orcid: <https://orcid.org/> .

orcid:0000-0001-2345-6789 foaf:homepage "https://example.org" ;
    foaf:name "M. Mustermann",
        "Maria Mustermann" .

orcid:0000-0002-7051-6198 foaf:homepage "http://jbusse.de" ;
    foaf:name "Johannes Busse" .