rdflib-jsonld icon indicating copy to clipboard operation
rdflib-jsonld copied to clipboard

Triples not loaded when using @context and @graph

Open algrebe opened this issue 8 years ago • 10 comments

I tried converting the following rdfa to jsonld on rdf translator.

<body>   
<div typeof="rdfs:Class" resource="http://schema.org/CreativeWork">
      <span class="h" property="rdfs:label">CreativeWork</span>
      <span property="rdfs:comment">The most generic kind of creative work, including books, movies, photographs, software programs, etc.</span>
       <span>Subclass of: <a property="rdfs:subClassOf" href="http://schema.org/Thing">Thing</a></span>
       <span>Source:  <a property="dc:source" href="http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_rNews">rNews</a></span>
    </div>

    <div typeof="rdfs:Class" resource="http://schema.org/WebPage">
      <span class="h" property="rdfs:label">WebPage</span>
      <span property="rdfs:comment">A web page. Every web page is implicitly assumed to be declared to be of type WebPage, so the various properties about that webpage, such as &lt;code&gt;breadcrumb&lt;/code&gt; may be used. We recommend explicit declaration if these properties are specified, but if they are found outside of an itemscope, they will be assumed to be about the page.</span>
       <span>Subclass of: <a property="rdfs:subClassOf" href="http://schema.org/CreativeWork">CreativeWork</a></span>
    </div>
</body>

The result was the following -

{
  "@context": {
    "dc": "http://purl.org/dc/terms/",
    "dcterms": "http://purl.org/dc/terms/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
    "schema": "http://schema.org/",
    "xsd": "http://www.w3.org/2001/XMLSchema#"
  },
  "@graph": [
    {
      "@id": "schema:CreativeWork",
      "@type": "rdfs:Class",
      "dcterms:source": {
        "@id": "http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_rNews"
      },
      "rdfs:comment": "The most generic kind of creative work, including books, movies, photographs, software programs, etc.",
      "rdfs:label": "CreativeWork",
      "rdfs:subClassOf": {
        "@id": "schema:Thing"
      }
    },
    {
      "@id": "schema:WebPage",
      "@type": "rdfs:Class",
      "rdfs:comment": "A web page. Every web page is implicitly assumed to be declared to be of type WebPage, so the various properties about that webpage, such as <code>breadcrumb</code> may be used. We recommend explicit declaration if these properties are specified, but if they are found outside of an itemscope, they will be assumed to be about the page.",
      "rdfs:label": "WebPage",
      "rdfs:subClassOf": {
        "@id": "schema:CreativeWork"
      }
    }
  ]
}

However, when I try to load this json-ld using the following

import rdflib
g = rdflib.Graph()
# converted.jsonld is the file I saved the above output to.
g.load("converted.jsonld", format="json-ld")
list(g.triples((None, None, None)))
# the output is []

algrebe avatar Apr 19 '16 08:04 algrebe

I tried parsing the rdfa using rdflib, and then serializing with json-ld.

import rdflib
g = rdflib.Graph()
g.load("input.rdfa", format="rdfa")
with open("output.jsonld", "w") as fp:
    fp.write(g.serialize(format="json-ld", indent=4))
cat output.jsonld
[
    {
        "@id": "http://schema.org/CreativeWork",
        "@type": [
            "http://www.w3.org/2000/01/rdf-schema#Class"
        ],
        "http://purl.org/dc/terms/source": [
            {
                "@id": "http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_rNews"
            }
        ],
        "http://www.w3.org/2000/01/rdf-schema#comment": [
            {
                "@value": "The most generic kind of creative work, including books, movies, photographs, software programs, etc."
            }
        ],
        "http://www.w3.org/2000/01/rdf-schema#label": [
            {
                "@value": "CreativeWork"
            }
        ],
        "http://www.w3.org/2000/01/rdf-schema#subClassOf": [
            {
                "@id": "http://schema.org/Thing"
            }
        ]
    },
    {
        "@id": "http://schema.org/WebPage",
        "@type": [
            "http://www.w3.org/2000/01/rdf-schema#Class"
        ],
        "http://www.w3.org/2000/01/rdf-schema#comment": [
            {
                "@value": "A web page. Every web page is implicitly assumed to be declared to be of type WebPage, so the various properties about that webpage, such as <code>breadcrumb</code> may be used. We recommend explicit declaration if these properties are specified, but if they are found outside of an itemscope, they will be assumed to be about the page."
            }
        ],
        "http://www.w3.org/2000/01/rdf-schema#label": [
            {
                "@value": "WebPage"
            }
        ],
        "http://www.w3.org/2000/01/rdf-schema#subClassOf": [
            {
                "@id": "http://schema.org/CreativeWork"
            }
        ]
    }
]

Parsing the above output.jsonld works perfectly fine. The difference between this and the original jsonld I've posted is the @context and @graph .

algrebe avatar Apr 19 '16 08:04 algrebe

When defining the context somewhere else, and specifying it in each of the objects in the list using @context, it still works -

[   
    { 
      "@context": "schema.context",
      "@id": "schema:CreativeWork",
      "@type": "rdfs:Class",
      "dcterms:source": {
        "@id": "http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#source_rNews"
      },
      "rdfs:comment": "The most generic kind of creative work, including books, movies, photographs, software programs, etc.",
      "rdfs:label": "CreativeWork",
      "rdfs:subClassOf": {
        "@id": "schema:Thing"
      }
    },
    { 
      "@context": "schema.context",
      "@id": "schema:WebPage",
      "@type": "rdfs:Class",
      "rdfs:comment": "A web page. Every web page is implicitly assumed to be declared to be of type WebPage, so the various properties about that webpage, such as <code>breadcrumb</code> may be used. We recommend explicit declaration if these properties are specified, but if they are found outside of an itemscope, they will be assumed to be about the page.",
      "rdfs:label": "WebPage",
      "rdfs:subClassOf": {
        "@id": "schema:CreativeWork"
      }
    }
]

How can I avoid writing @context in each of the objects ?

algrebe avatar Apr 19 '16 08:04 algrebe

Currently experiencing similar trouble in parsing some json-ld making use of "@graph" to attach a common "@context" to a list of identically structured objects.

ant1b avatar Apr 20 '16 15:04 ant1b

Similar problem here also. Cannot parse back the serialized json-ld (serialized by rdflib-jsonld).

ddxgz avatar Sep 12 '16 13:09 ddxgz

It seems to work when using ConjunctiveGraph instead of Graph.

fserena avatar Dec 12 '16 13:12 fserena

@fserena ConjunctiveGraph works! I created g=ConjunctiveGraph and it now works perfectly fine

jangwhan avatar Jan 22 '17 16:01 jangwhan

Example 61 from https://www.w3.org/TR/json-ld/ gives an empty graph for me, even with ConjunctiveGraph.

kud1ing avatar Feb 20 '17 09:02 kud1ing

#53 seems to be the same issue. The suggested fix from @niklasl there also uses ConjunctiveGraph but is not working for me. See also https://github.com/RDFLib/rdflib/issues/436

danbri avatar Mar 27 '18 12:03 danbri

Maybe this helps: I got it working, only when I stopped using the method-result chaining shortcut syntax:

foo = ConjunctiveGraph() bar = foo.parse(data=mydoor, format='json-ld') foo.serialize()

This works. However the object "bar" returned from calling 'parse' is the wrong/empty graph, so we can't chain the expressions together with ConjunctiveGraph().parse(etc...).

danbri avatar Mar 27 '18 12:03 danbri

Maybe this helps: I got it working, only when I stopped using the method-result chaining shortcut syntax:

foo = ConjunctiveGraph() bar = foo.parse(data=mydoor, format='json-ld') foo.serialize()

This works. However the object "bar" returned from calling 'parse' is the wrong/empty graph, so we can't chain the expressions together with ConjunctiveGraph().parse(etc...).

Thanks, It's work fine for me! Don't use the chain expressions together with ConjunctiveGraph().parse(etc...).

Chiang97912 avatar Mar 23 '20 18:03 Chiang97912