Search code examples
pythonjson-ldrdflibturtle-rdf

Providing the @type in the @context for a value


I have the following json-ld document:

{
     "@context": {
        "ex": "http://example.com/",
        "yyyy": "ex:yyyy",
        "name": "ex:name",
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
        "sch": "http://schema.org/",
        "xml": "http://www.w3.org/XML/1998/namespace",
        "xsd": "http://www.w3.org/2001/XMLSchema#"
    },
    "@id": "ex:Bobe",
    "@type": "ex:MyType",
    "yyyy": {
        "@type": "ex:XXXX",
        "name": "my name"
    }
}

The RDF representation is:

@prefix ex: <http://example.com/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sch: <http://schema.org/> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:Bobe a ex:MyType ;
    ex:yyyy [ a ex:XXXX ;
            ex:name "my name" ] .

What I would like to be able to do is write "yyyy": { ... } as

"yyyy": {
    "name": "my name"
}

and have "@type": "ex:XXXX" specified in the "@context".

Is this possible?

What I tried, but didn't expect to work, was:

{
    "@context": {
        "ex": "http://example.com/",
        "yyyy": {
            "@id": "ex:yyyy",
            "@type": "ex:XXXX"
        },
        "name": "ex:name",
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
        "sch": "http://schema.org/",
        "xml": "http://www.w3.org/XML/1998/namespace",
        "xsd": "http://www.w3.org/2001/XMLSchema#"
    },
    "@id": "ex:Bobe",
    "@type": "ex:MyType",
    "yyyy": {
        "name": "my name"
    }
}

and this has an RDF representation of:

@prefix ex: <http://example.com/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sch: <http://schema.org/> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:Bobe a ex:MyType ;
    ex:yyyy [ ex:name "my name" ] .

The N-Quad representation on the JSON-LD Playground is:

<http://example.com/Bobe> <http://example.com/yyyy> _:b0 .
<http://example.com/Bobe> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/MyType> .
_:b0 <http://example.com/name> "my name" .

So, the "@type" information is lost.

It would need to, of course, work in the situation where I had:

{
    "@context": {
        "ex": "http://example.com/",
        "yyyy": {
            "@id": "ex:yyyy",
            "@type": "ex:XXXX"
        },
        "name": "ex:name",
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
        "sch": "http://schema.org/",
        "xml": "http://www.w3.org/XML/1998/namespace",
        "xsd": "http://www.w3.org/2001/XMLSchema#"
    },
    "@id": "ex:Bobe",
    "@type": "ex:MyType",
    "yyyy": [ { "name": "my name" },
              { "name": "my other" } ]
}

I am thinking this is not possible, but would like to confirm.

The python code generally being used to generate this output is:

graph_data = """
{
    "@id": "ex:Bobe",
    "@type": "ex:MyType",
    "@context": {
        "ex": "http://example.com/",
        "yyyy": "ex:yyyy",
        "name": "ex:name",
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
        "sch": "http://schema.org/",
        "xml": "http://www.w3.org/XML/1998/namespace",
        "xsd": "http://www.w3.org/2001/XMLSchema#"
    },
    "yyyy": {
        "@type": "ex:XXXX",
        "name": "my name"
    }
}
"""

print( graph_data )

data  = rdflib.Graph().parse( data = graph_data, format = 'json-ld' )
print( f"{data.serialize( format = 'ttl' ).decode( 'utf8' )}" )

Solution

  • From https://www.w3.org/TR/json-ld11/#context-definitions:

    "A context definition MUST be a map whose keys MUST be either terms, compact IRIs, IRIs, or one of the keywords @base, @import, @language, @propagate, @protected, @type, @version, or @vocab. "

    So @context really doesn't know nything about nested information as you have it with

    "yyyy": {
        "@id": "ex:yyyy",
        "@type": "ex:XXXX"
    },
    

    @context can't be used for structure, just namespace management