Search code examples
rdfjson-ld

How to encode datatype IRIs for RDF values in JSON-LD?


A JSON-LD context can be used to specify the range of a property. E.g., the following stats that the range of rdf:value consists of integers:

{
  "@context": {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "xsd": "http://www.w3.org/2001/XMLSchema#",
    "rdf:value": { "@type": "xsd:integer" }
  },
  "rdf:value": "1"
}

​In RDF modeling it is common to use different ranges for different uses of rdf:value. E.g., the following expresses that an object costs €2,50 and has temperature 28.2 ℃ (using Turtle notation):

_:1 ex:price [ rdf:value "2.50"​^^xsd:decimal ; ex:unit ex:euros ] ;
    ex:temperature [ rdf:value "28.2"^^xsd:float ; ex:unit ex:degreesCelsius ] .

How do I describe this in terms of a JSON-LD context? It seems to me that I need property paths (borrowing a concept from SPARQL) as keys, specifically the following for the current example:

"ex:price/rdf:value": "xsd:decimal"
"ex:temperature/rdf:value": "xsd:float"

Is there a way to specify this in JSON-LD?


Solution

  • You can also nest @context to specialize/override properties. To take your example:

    {
      "@context": {
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "xsd": "http://www.w3.org/2001/XMLSchema#",
        "rdf:value": { "@type": "xsd:integexr" }
      },
      "rdf:value": "1",
      "ex:price": {
        "@context": {
          "rdf:value": { "@type": "xsd:float"}
        },
        "rdf:value": "35.3"
      },
      "ex:temperature": {
        "@context": {
          "rdf:value": { "@type": "xsd:decimal"}
        },
        "rdf:value": "2.50"
      }
    }
    

    You can experiment with this in the JSON-LD Playground.

    Another approach is to use custom properties that all map to one @id (rdf:value) but with different datatypes:

    {
      "@context": {
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "xsd": "http://www.w3.org/2001/XMLSchema#",
        "value_integer": {
          "@id": "rdf:value",
          "@type": "xsd:integer"
        },
        "value_float": {
          "@id": "rdf:value",
          "@type": "xsd:float"
        },
        "value_decimal": {
          "@id": "rdf:value",
          "@type": "xsd:decimal"
        }
      },
      "value_integer": "1",
      "ex:price": {
        "value_decimal": "35.3"
      },
      "ex:temperature": {
        "value_float": "2.50"
      }
    }
    

    See this example on the JSON-LD playground.