Skip to main content

JSON-LD Context

info

This section is in draft. The content and structure of the document may change until it is released.

A JSON-LD context is a mapping of the JSON-LD terms (properties) to their respective RDF terms. The JSON-LD context is very powerful and can perform value type coercion when used with a JSON-LD processor. The advantage of using a JSON-LD context is to reduce complexity and verbosity of the JSON-LD data.

A JSON-LD context example taken from json-ld#the-context.

{
"@context": {
"name": "http://schema.org/name",
↑ This means that 'name' is shorthand for 'http://schema.org/name'
"image": {
"@id": "http://schema.org/image",
↑ This means that 'image' is shorthand for 'http://schema.org/image'
"@type": "@id"
↑ This means that a string value associated with 'image'
should be interpreted as an identifier that is an IRI
},
"homepage": {
"@id": "http://schema.org/url",
↑ This means that 'homepage' is shorthand for 'http://schema.org/url'
"@type": "@id"
↑ This means that a string value associated with 'homepage'
should be interpreted as an identifier that is an IRI
}
}
}

The following JSON-LD does not use a JSON-LD context. It is quite verbose since some of the properties are fully qualified URIs.

{
"@id": "https://example.com/dataset/1",
"@type": "https://w3id.org/tern/ontologies/tern/RDFDataset",
"http://purl.org/dc/terms/title": "Example Dataset 1",
"http://purl.org/dc/terms/issued": {
"@type": "http://www.w3.org/2001/XMLSchema#date",
"@value": "2021-10-20"
},
"http://purl.org/dc/terms/description": "Example dataset description."
}

The following JSON-LD uses a JSON-LD context. Notice that the data in the @graph can use simple terms instead of fully qualified URIs and the value of issued is coerced to an http://www.w3.org/2001/XMLSchema#date.

{
"@context": {
"title": {
"@id": "http://purl.org/dc/terms/title",
"@type": "http://www.w3.org/2001/XMLSchema#string"
},
"issued": {
"@id": "http://purl.org/dc/terms/issued",
"@type": "http://www.w3.org/2001/XMLSchema#date"
},
"description": {
"@id": "http://purl.org/dc/terms/description",
"@type": "http://www.w3.org/2001/XMLSchema#string"
}
},
"@graph": {
"@id": "https://example.com/dataset/1",
"@type": "https://w3id.org/tern/ontologies/tern/RDFDataset",
"title": "Example Dataset 1",
"issued": "2021-10-20",
"description": "Example dataset description."
}
}

The advantages may not be obvious on this small data example, but from practical experience, using a JSON-LD context

JSON-LD Context definition

In JSON-LD, a context is used to map terms, i.e., properties with associated values in an JSON document, to URLs. A term is a short word that expands to a URL. Terms may be defined as any valid JSON string other than a JSON-LD keyword.

Source: https://json-ld.org/spec/latest/json-ld-api-best-practices/#dfn-json-ld-context

The JSON-LD Context used in this document's examples

The JSON-LD code snippets throughout this section of the developer guide assumes the following context:

{
"@context": {
"featureType": {
"@id": "https://w3id.org/tern/ontologies/tern/featureType",
"@type": "@id"
},
"hasFeatureOfInterest": {
"@id": "http://www.w3.org/ns/sosa/hasFeatureOfInterest",
"@type": "@id"
},
"observedProperty": {
"@id": "http://www.w3.org/ns/sosa/observedProperty",
"@type": "@id"
},
"usedProcedure": {
"@id": "http://www.w3.org/ns/sosa/usedProcedure",
"@type": "@id"
},
"hasResult": {
"@id": "http://www.w3.org/ns/sosa/hasResult",
"@type": "@id"
},
"value": {
"@id": "http://www.w3.org/1999/02/22-rdf-syntax-ns#value"
},
"vocabulary": {
"@id": "https://w3id.org/tern/ontologies/tern/vocabulary",
"@type": "@id"
},
"phenomenonTime": {
"@id": "http://www.w3.org/ns/sosa/phenomenonTime"
},
"resultTime": {
"@id": "http://www.w3.org/ns/sosa/resultTime",
"@type": "http://www.w3.org/2001/XMLSchema#dateTime"
},
"inDataset": {
"@id": "http://rdfs.org/ns/void#inDataset",
"@type": "@id"
},
"hasSimpleResult": {
"@id": "http://www.w3.org/ns/sosa/hasSimpleResult"
},
"isResultOf": {
"@id": "http://www.w3.org/ns/sosa/isResultOf",
"@type": "@id"
},
"inXSDDateTimeStamp": {
"@id": "http://www.w3.org/2006/time#inXSDDateTimeStamp"
},
"unit": {
"@id": "https://w3id.org/tern/ontologies/tern/unit",
"@type": "@id"
},
"hasGeometry": {
"@id": "http://www.opengis.net/ont/geosparql#hasGeometry",
"@type": "@id"
},
"label": {
"@id": "http://www.w3.org/2000/01/rdf-schema#label"
},
"dimension": {
"@id": "https://w3id.org/tern/ontologies/tern/dimension"
},
"locationDescription": {
"@id": "https://w3id.org/tern/ontologies/tern/locationDescription"
},
"siteDescription": {
"@id": "https://w3id.org/tern/ontologies/tern/siteDescription"
},
"polygon": {
"@id": "https://w3id.org/tern/ontologies/tern/polygon",
"@type": "@id"
},
"type": {
"@id": "http://purl.org/dc/terms/type",
"@type": "@id"
},
"asWKT": {
"@id": "http://www.opengis.net/ont/geosparql#asWKT",
"@type": "http://www.opengis.net/ont/geosparql#wktLiteral"
},
"lat": {
"@id": "http://www.w3.org/2003/01/geo/wgs84_pos#lat"
},
"long": {
"@id": "http://www.w3.org/2003/01/geo/wgs84_pos#long"
},
"pointType": {
"@id": "https://w3id.org/tern/ontologies/loc/pointType",
"@type": "@id"
},
"title": {
"@id": "http://purl.org/dc/terms/title",
"@type": "http://www.w3.org/2001/XMLSchema#string"
},
"issued": {
"@id": "http://purl.org/dc/terms/issued"
},
"description": {
"@id": "http://purl.org/dc/terms/description",
"@type": "http://www.w3.org/2001/XMLSchema#string"
}
}
}