JSON-LD Context
JSON-LD is a JSON-based format to serialize Linked Data. It adds semantics to a JSON document by introducing new keywords and allows one to embed a JSON-LD context to the document that defines the vocabulary used in the document. For instance, here is a simple JSON-LD document.
The @context
value defines the JSON-LD context used in the document. Here it defines that the foo
term designates the https://example.com/property
. This means that this document is semantically equivalent to the following:
TreeLDR allows you to generate JSON-LD contexts from a TreeLDR schema, and import some JSON-LD contexts that can be interpreted as a schema into a TreeLDR schema.
Generating a JSON-LD Context
For now, only type scoped contexts are supported, even if no rdf:a
property is present in the layout.
Generating a JSON-LD context is done by using the json-ld-context
subcommand of the command line utility after the list of inputs, and by specifying what layout should be embedded into the context.
This context can then be safely embedded into a BasicPost
JSON-LD instance as its context:
Type Scoped Contexts
In the example above, properties are defined for the entire document. Their scope is not limited to nodes representing BasicPost
objects, and could potentially conflict with other term definitions. To avoid this, JSON-LD allows us to define type scoped contexts that only apply to nodes of a given type. The downside is that the type must appear in the node itself.
One can ensure that by adding the rdf:type
property to the layout.
This will generate the following context, with the BasicPost
type scoped context.
The name of the layout is used to define the type scoped context. The rdf:type
property has been translated into a @type
keyword alias. This can be avoided by renaming the property into the keyword itself:
This will generate a similar context, but without the type
term definition.
This context can then be safely embedded into a BasicPost
JSON-LD instance as its context:
Note how the @type
field with the BasicPost
value is now required to use the type scoped context.
Importing a JSON-LD Context
JSON-LD context import is not implemented yet.
Last updated