Skip to content

JSON-LD

Are you tired of using 🐢 Turtles to write models? You are more familiar with JSON and YAML!

JSON-LD is a W3C standard to map JSON (and therefore YAML) to the RDF linked data model.

YAML

We can rewrite the greeting2.ttl from the Linked Data chapter as this greeting2.yaml:

id: greeting2
message: "hello"
object: world

---
id: world
wikipedia: https://en.wikipedia.org/wiki/Earth

Of course, we lost what uniquely identified our things… but JSON-LD Contexts can re-provide that, using this greeting-context.jsonld:

{
  "@context": {
    "@version": 1.1,
    "@vocab": "https://example.org/",
    "@base": "https://example.org/",
    "id": "@id",
    "object": {
      "@type": "@id"
    },
    "wikipedia": {
      "@type": "@id"
    }
  }
}

Combining these, Enola can recreate the same data model:

$ ./enola get --load="models/example.org/greeting2.yaml?context=models/example.org/greeting-context.jsonld" https://example.org/greeting2
iri: https://example.org/greeting2
properties:
  https://example.org/message: {string: hello}
  https://example.org/object: {link: 'https://example.org/world'}
  https://enola.dev/origin: {link: 'file:///home/runner/work/enola/enola/models/example.org/greeting2.yaml?context=models%2Fexample.org%2Fgreeting-context.jsonld'}

Generating documentation will look the same as in the original Linked Data chapter, using this:

$ ./enola docgen --load="models/example.org/greeting2.yaml?context=models/example.org/greeting-context.jsonld" --output=/tmp/models/ --no-index

More

Tools > JSON-LD has more reference documentation about JSON-LD in Enola.