Datamodelling with a “Json to Rdf” Approach.

At the example of this stackoverflow question:

“Supposing we have the following triple in Turtle syntax:

<http:/example.com/Paul> <http:/example.com/running> <http:/example.com/10miles> .

How do I add a start and end time? For example if I want to say he started at 10 am and finished his 10miles run at 12 am. I want to use xsd:dateTime.”

https://stackoverflow.com/questions/49726990


Sometimes it can be hard to create good, well fitting models. In my own experience it is crucial to identify a well defined set of entities and relations to create a vocabulary from.  Some people prefer to use visual strategies to develop their models. I prefer to write models in structured text.  This has the advantage that the process of modeling directly leads into actual coding.

Here is an example on how I would tackle the question .

1. The modelling part (not much RDF involved)

{
    "runs": [
        {
            "id": "runs:0000001",
            "distance": {
                "length": 10.0,
                "unit": "mile"
            },
            "time": {
                "start": "2018-04-09T10:00:00",
                "end": "2018-04-09T12:00:00"
            },
            "runner": {
                "id": "runner:0000002",
                "name": "Paul"
            }
        }
    ]
}

We store the json document in a file run.json. From here we can use the ‘oi’ command line tool , to create an adhoc context.

oi run.json -t context

The resulting context is just a stub. But with a few additions we can easily create a context document to define id’s and types for each vocable/entity/relation.

2. The RDF part: define a proper context for your document.

   {
    "@context": {
        "ical": "http://www.w3.org/2002/12/cal/ical#",
        "xsd": "http://www.w3.org/2001/XMLSchema#",
        "runs": {
            "@id": "info:stack/49726990/runs/",
            "@container": "@list"
        },
        "distance": {
            "@id": "info:stack/49726990/distance"
        },
        "length": {
            "@id": "info:stack/49726990/length",
            "@type": "xsd:double"
        },
        "unit": {
            "@id": "info:stack/49726990/unit"
        },
        "runner": {
            "@id": "info:stack/49726990/runner/"
        },
        "name": {
            "@id": "info:stack/49726990/name"
        },
        "time": {
            "@id": "info:stack/49726990/time"
        },
        "start": {
            "@id":"ical:dtstart",
            "@type": "xsd:dateTime"
        },
        "end": {
            "@id":"ical:dtend",
            "@type": "xsd:dateTime"
        },
        "id": "@id"
    }
}

3. The fun part: Throw it to an RDF converter of your choice

This is how it looks in JSON-Playground

Or simply use ‘oi’:

 
oi run.json -f run.context -t ntriples

Prints:

 
_:b0 <info:stack/49726990/runs/> _:b3 .
_:b3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> <info:stack/49726990/runs/0000001> .
_:b3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
<info:stack/49726990/runs/0000001> <info:stack/49726990/distance> _:b1 .
<info:stack/49726990/runs/0000001> <info:stack/49726990/runner/> <info:stack/49726990/runner/0000002> .
<info:stack/49726990/runs/0000001> <info:stack/49726990/time> _:b2 .
_:b1 <info:stack/49726990/length> "1.0E1"^^<http://www.w3.org/2001/XMLSchema#double> .
_:b1 <info:stack/49726990/unit> "mile" .
<info:stack/49726990/runner/0000002> <info:stack/49726990/name> "Paul" .
_:b2 <http://www.w3.org/2002/12/cal/ical#dtend> "2018-04-09T12:00:00"^^<http://www.w3.org/2001/XMLSchema#dateTime> .
_:b2 <http://www.w3.org/2002/12/cal/ical#dtstart> "2018-04-09T10:00:00"^^<http://www.w3.org/2001/XMLSchema#dateTime> .

Leave a Reply