JSON-LD context
Generate a JSON-LD 1.1 context or frame for the JSON data of the schema.
ohi get w3c/prov-oohi linkml to jsonld-context --full-uris \ --to prov-o.context.jsonld prov-o.linkml.yamlimport ohi_linkml
with ohi_linkml.load_file("prov-o.linkml.yaml") as schema: output = schema.jsonld_context(full_uris=True)import { readFileSync } from "node:fs";import { LinkML } from "@neverblink/linkml";
const yaml = readFileSync("prov-o.linkml.yaml", "utf8");const { view, report } = LinkML.loadFromString(yaml, {});if (!view) throw new Error(JSON.stringify(report));const output = view.jsonldContext({ full_uris: true });use ohi_linkml::{load_file, JsonLdContextGeneratorOptions};
let schema = load_file("prov-o.linkml.yaml", &Default::default())?;let output = schema.jsonld_context(&JsonLdContextGeneratorOptions { full_uris: true, ..Default::default()})?;#include <stddef.h>#include "ohi_linkml.h"
char *report = NULL, *error = NULL;long long schema = linkml_load_file("prov-o.linkml.yaml", NULL, &report, &error);char *output = linkml_jsonld_context( schema, "{\"full_uris\": true}", &error);linkml_free(output);linkml_free(report);linkml_free(error);linkml_close(schema);import eu.neverblink.linkml.generator.options.*import eu.neverblink.linkml.generator.jsonldcontext.JsonLdContextGeneratorimport eu.neverblink.linkml.schemaview.{SchemaIssues, SchemaView}
val loaded = SchemaView.loadSchemaViewFromUri("prov-o.linkml.yaml")given SchemaView = SchemaIssues.orThrow(loaded)val output = JsonLdContextGenerator().serialize( JsonLdContextGeneratorOptions(fullUris = true),)The context is for documents whose top object is the tree root class, or a list of such objects.
Options
Section titled “Options”context_urlstring--context-urlScala contextUrlWith frame, the URL of the context, to put in the frame instead of the context.
framebooleandefault false--frameWhether to write a JSON-LD frame instead of the context. The frame turns RDF of the schema's data back into JSON of the same shape.
full_urisbooleandefault false--full-urisScala fullUrisWhether to write URIs in full, instead of compacting them to CURIEs with the schema's prefixes.
indentation_stepintegerdefault 2--indentation-stepScala indentationStepThe number of spaces for each level of indentation in the output.
tree_rootstring--tree-rootScala treeRootThe class at the top of the JSON documents, instead of the class with tree_root: true.
Mapping
Section titled “Mapping”| LinkML | JSON-LD |
|---|---|
| Slot | Term for its alias or name, with the slot URI as @id |
| Identifier | Alias of @id |
| Type designator | Alias of @type, plus a term for each class |
Reference, uri, curie, uriorcurie |
"@type": "@id" |
Other types, except integer and boolean |
"@type" with the type’s URI |
| Enum | "@type": "@vocab", with a term for each value |
implicit_prefix |
"@type": "@vocab", with the prefix as @vocab |
langstring |
"@container": "@language" |
| Dict keyed by the identifier | "@container": "@id" |
Dict keyed by a key slot |
"@container": "@index" |
| Other multivalued slots | "@container": "@set" |
A key with different meanings in different classes gets a scoped context.
Limits
Section titled “Limits”The RDF made with the context won’t have rdf:type for an object, unless the JSON has a type designator. The generator lists everything else a context could not express:
- An identifier of a string range doesn’t also become a literal.
- An identifier or reference that is a number or boolean isn’t an IRI.
- An identifier with an
implicit_prefixis resolved against the base IRI. - A SimpleDict with plain values, and a tree root that is a dict.
- A value of range
Any. - A list that mixes plain texts and texts in several languages.
- A
decimalwith a fraction, written as"1.5E0"^^xsd:decimal. - Ranges that need different terms, such as
stringanduri. - A key or enum value with
:or/that isn’t its own IRI. - A prefix with the same name as a key.
- A key with different meanings in different classes, without a tree root, or in objects nested in each other.
- Too many such keys: scoped contexts stop at 10,000 terms.
Frames
Section titled “Frames”When the frame option is enabled, the generator writes a JSON-LD frame with the context in it. Alternatively, you can reference an external context by setting the context_url option. Framing RDF of the schema’s data gives JSON of the same shape as the schema’s JSON, with @type on each object. Frame with native types, such as useNativeTypes in JSON-LD processors.
The frame matches objects of the tree root class, embeds inlined objects, and leaves references as IRIs. Besides the limits above, these don’t come back as they were:
float,doubleanddecimalvalues. Their key is the slot’s full IRI instead of its name.- A number or boolean in a slot whose other ranges are enums or references.
- Text without a language, which ends up under
@none. urivalues, which can become CURIEs.- String identifiers, and references to them, which become full IRIs.
- Objects of the tree root class inside other objects, which also show up at the top.
The generator emits a warning every time it encounters one of these limitations.
Warnings
Section titled “Warnings”The CLI prints the warnings to stderr. In the bindings, jsonld_context_with_warnings (jsonldContextWithWarnings in JavaScript) returns the document with its warnings.