JSON-LD Tutorial

Item:OSW911488771ea449a6a34051f8213d7f2f /
Revision as of 07:27, 13 April 2023 by Admin (talk | contribs) (Install package: OSW Docs - Core)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
JSON-LD Tutorial [OSW911488771ea449a6a34051f8213d7f2f]
ID OSW911488771ea449a6a34051f8213d7f2f
UUID 91148877-1ea4-49a6-a340-51f8213d7f2f
Label JSON-LD Tutorial
Machine compatible name JsonLdTutorial
OSWcdf1d2e2f5d143aeb13096013632c8cb.png
Statements (outgoing)
Statements (incoming)
Keywords

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) JSON Application Tutorial

View as slide show

JSON-LD: Motivation

Problem: Everybody uses different keys

JSON Dataset 1
{
    "c": 1.0
}
JSON Dataset 2
{
    "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

JSON Dataset 1
{
    "@context": {
        "c": "http://qudt.org/vocab/quantitykind/Capacitance"  
    },
    "c": 1.0
}
JSON Dataset 2
{    
    "@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
{
    "@context": [
        "http://myschema.org/allmyquantitites.jsonld"
        {
            "current": "qudt:Current",        
        }
    ],
    "cap": 1.0,
    "current": 10.0 
}

JSON-LD: Mapping (3) - Nested context

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

Playground

Input
{
  "@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
  }
}

=>

Flattened output
{
  "@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

Playground

Input
{
  "@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"
    }
  ]
}
Frame
{
  "@context": {
    "@version": 1.1,
    "@vocab": "http://example.org/"
  },
  "@type": "Model",
  "contains": {
    "@type": "Submodel",
    "contains": {
      "@type": "Submodel"
    },
      "hasParameter": {
    "@type": "Parameter"
  }
  },
  "hasParameter": {
    "@type": "Parameter"
  }
}
Framed output
{
  "@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
"Item:OSWf1df064239044b8fa3c968339fb93344"
optional_predecessor
"Item:OSWf4a9514baed04859a4c6c374a7312f10"
type
"Category:OSW494f660e6a714a1a9681c517bbb975da"
uuid"91148877-1ea4-49a6-a340-51f8213d7f2f"
name"JsonLdTutorial"
label
text"JSON-LD Tutorial"
lang"en"
image"File:OSWcdf1d2e2f5d143aeb13096013632c8cb.png"
recommended_successor
"Item:OSWee501c0fa6a9407d99c058b5ff9d55b4"