Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Triples not loaded when using @context and @graph #40

Open
algrebe opened this issue Apr 19, 2016 · 10 comments
Open

Triples not loaded when using @context and @graph #40

algrebe opened this issue Apr 19, 2016 · 10 comments

Comments

@algrebe
Copy link

algrebe commented Apr 19, 2016

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
Copy link
Author

algrebe commented Apr 19, 2016

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
Copy link
Author

algrebe commented Apr 19, 2016

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 ?

@ant1b
Copy link

ant1b commented Apr 20, 2016

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.

@ddxgz
Copy link

ddxgz commented Sep 12, 2016

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

@fserena
Copy link

fserena commented Dec 12, 2016

It seems to work when using ConjunctiveGraph instead of Graph.

@jangwhan
Copy link

jangwhan commented Jan 22, 2017

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

@kud1ing
Copy link

kud1ing commented Feb 20, 2017

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

@danbri
Copy link

danbri commented Mar 27, 2018

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

@danbri
Copy link

danbri commented Mar 27, 2018

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...).

metatoaster added a commit to PMR2/pmr2.virtuoso that referenced this issue May 2, 2019
- This will likely be a case that impacts anticipated usage.
@Chiang97912
Copy link

Chiang97912 commented Mar 23, 2020

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...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants