JSON-LD Tutorial
Description
No description found
Item | |
---|---|
Type(s)/Category(s) | Tutorial |
CreativeWork |
---|
Article |
---|
Tutorial | |
---|---|
Prerequisites (required) | JSON Tutorial |
Prerequisites (optional) | JSON-SCHEMA Tutorial |
Follow-up (recommended) | OO-LD Tutorial |
JSON-LD: Motivation
Problem: Everybody uses different keys
{
"c": 1.0
}
{
"cap": 1.0
}
Do they mean the same?
JSON-LD: Mapping (1)
Idea: Map keys to common vocabularies like https://www.qudt.org/doc/DOC_VOCAB-QUANTITY-KINDS.html
{
"@context": {
"c": "http://qudt.org/vocab/quantitykind/Capacitance"
},
"c": 1.0
}
{
"@context": {
"cap": "http://qudt.org/vocab/quantitykind/Capacitance"
},
"cap": 1.0
}
Do they mean the same? Yes!
JSON-LD: Mapping (2)
JSON-LD allows the definition of prefixes
{
"@context": {
"qudt": "http://qudt.org/vocab/quantitykind/",
"cap": "qudt:Capacitance",
},
"cap": 1.0
}
JSON-LD: Mapping (3) - Remote context
JSON-LD allows the import of context(s) from other JSON documents
- Prefixes and mappings can be moved to a remote context
http://myschema.org/allmyquantitites.jsonld
{
"@context": {
"qudt": "http://qudt.org/vocab/quantitykind/",
"cap": "qudt:Capacitance",
}
}
{
"@context": [
"http://myschema.org/allmyquantitites.jsonld"
{
"current": "qudt:Current",
}
],
"cap": 1.0,
"current": 10.0
}
JSON-LD: Mapping (4) - Full example
Actually this example was oversimplified. A full semantic representation would be
http://myschema.org/allmyquantitites.jsonld
{
"@context": {
"qudt": "http://qudt.org/schema/qudt/",
"qunit": "http://qudt.org/vocab/unit/",
"qkind": "http://qudt.org/vocab/quantkind/",
"unit": {
"@id": "qudt:hasUnit",
"@type": "@id"
},
"value": "qudt:value"
}
}
{
"@context": "http://myschema.org/allmyquantitites.jsonld",
"value": 4.0,
"unit": "qunit:CentiM"
}
which would translate to <pint.Quantity(4.0, 'centimeter')>
in python, see [1]
JSON-LD: Advanced concepts
There are many more feature of JSON-LD - see specification [2]
- Property types, object properties
- Reverse Properties
- Arrays and containers
- Multi-language strings
- Framing
- ...
In general JSON-LD is not only about annotation but also transformation and normalization
JSON-LD: Scopes
A property / the related subobject can have it's own context. The global context is inherited. Playground
{
"@context": {
"test": "http://test.org/",
"id": "test:HasId",
"name": "test:HasName",
"subobject": {
"@id": "test:HasSubobject",
"@context": {
"id": "test:HasSubId"
}
}
},
"name": "Test 1",
"id": "1",
"subobject": {
"name": "Test 1.1",
"id": "1.1"
}
}
JSON-LD: Flatten a hierarchy
{
"@context": {
"@version": 1.1,
"@vocab": "http://example.org/"
},
"@id": "http://example.org/SM1",
"@type": "Model",
"contains": {
"@id": "http://example.org/SM2",
"@type": "Submodel",
"contains": {
"@id": "http://example.org/SM3",
"@type": "Submodel",
"contains": {
"@id": "http://example.org/SM4",
"@type": "Submodel",
"hasParameter": {
"@id": "http://example.org/P2",
"@type": "Parameter",
"title": "P2",
"value": 2
},
"title": "Submodel 4"
},
"hasParameter": {
"@id": "http://example.org/P3",
"@type": "Parameter",
"title": "P3",
"value": 3
},
"title": "Submodel 3"
},
"hasParameter": null,
"title": "Submodel 2"
},
"hasParameter": {
"@id": "http://example.org/P1",
"@type": "Parameter",
"title": "P1",
"value": 1
}
}
=>
{
"@graph": [
{
"@id": "http://example.org/P1",
"@type": "http://example.org/Parameter",
"http://example.org/title": "P1",
"http://example.org/value": 1
},
{
"@id": "http://example.org/P2",
"@type": "http://example.org/Parameter",
"http://example.org/title": "P2",
"http://example.org/value": 2
},
{
"@id": "http://example.org/P3",
"@type": "http://example.org/Parameter",
"http://example.org/title": "P3",
"http://example.org/value": 3
},
{
"@id": "http://example.org/SM1",
"@type": "http://example.org/Model",
"http://example.org/contains": {
"@id": "http://example.org/SM2"
},
"http://example.org/hasParameter": {
"@id": "http://example.org/P1"
}
},
{
"@id": "http://example.org/SM2",
"@type": "http://example.org/Submodel",
"http://example.org/contains": {
"@id": "http://example.org/SM3"
},
"http://example.org/title": "Submodel 2"
},
{
"@id": "http://example.org/SM3",
"@type": "http://example.org/Submodel",
"http://example.org/contains": {
"@id": "http://example.org/SM4"
},
"http://example.org/hasParameter": {
"@id": "http://example.org/P3"
},
"http://example.org/title": "Submodel 3"
},
{
"@id": "http://example.org/SM4",
"@type": "http://example.org/Submodel",
"http://example.org/hasParameter": {
"@id": "http://example.org/P2"
},
"http://example.org/title": "Submodel 4"
}
]
}
JSON-LD: Rebuild a hierarchy
{
"@context": {
"@vocab": "http://example.org/",
"ex": "http://example.org/",
"contains": {
"@type": "@id"
},
"hasParameter": {"@type": "@id"},
"parameterOf": {"@type": "@id", "@reverse": "hasParameter"},
"submodels": "@graph"
},
"@id": "ex:SM1",
"@type": "Model",
"contains": "ex:SM2",
"hasParameter": "ex:P1",
"submodels": [
{
"@id": "ex:SM2",
"@type": "Submodel",
"title": "Submodel 2",
"contains": "ex:SM3"
},
{
"@id": "ex:SM3",
"@type": "Submodel",
"title": "Submodel 3",
"contains": "ex:SM4"
},
{
"@id": "ex:SM4",
"@type": "Submodel",
"title": "Submodel 4",
"hasParameter": "ex:P2"
},
{
"@type": "Parameter",
"@id": "ex:P1",
"title": "P1",
"value": 1.0
},
{
"@type": "Parameter",
"@id": "ex:P2",
"title": "P2",
"value": 2.0
},
{
"@type": "Parameter",
"@id": "ex:P3",
"title": "P3",
"value": 3.0,
"parameterOf": "ex:SM3"
}
]
}
{
"@context": {
"@version": 1.1,
"@vocab": "http://example.org/"
},
"@type": "Model",
"contains": {
"@type": "Submodel",
"contains": {
"@type": "Submodel"
},
"hasParameter": {
"@type": "Parameter"
}
},
"hasParameter": {
"@type": "Parameter"
}
}
{
"@context": {
"@version": 1.1,
"@vocab": "http://example.org/"
},
"@id": "http://example.org/SM1",
"@type": "Model",
"contains": {
"@id": "http://example.org/SM2",
"@type": "Submodel",
"contains": {
"@id": "http://example.org/SM3",
"@type": "Submodel",
"contains": {
"@id": "http://example.org/SM4",
"@type": "Submodel",
"hasParameter": {
"@id": "http://example.org/P2",
"@type": "Parameter",
"title": "P2",
"value": 2
},
"title": "Submodel 4"
},
"hasParameter": {
"@id": "http://example.org/P3",
"@type": "Parameter",
"title": "P3",
"value": 3
},
"title": "Submodel 3"
},
"hasParameter": null,
"title": "Submodel 2"
},
"hasParameter": {
"@id": "http://example.org/P1",
"@type": "Parameter",
"title": "P1",
"value": 1
}
}
JSON Documents: Next
jsondata
required_predecessor |
| |||||
---|---|---|---|---|---|---|
optional_predecessor |
| |||||
type |
| |||||
uuid | "91148877-1ea4-49a6-a340-51f8213d7f2f" | |||||
name | "JsonLdTutorial" | |||||
label |
| |||||
image | "File:OSWcdf1d2e2f5d143aeb13096013632c8cb.png" | |||||
recommended_successor |
|