The following code should return a dictionary of the subject of all triples in the ontology. It, instead, returns the entire ontology as an XML string.
from SPARQLWrapper import SPARQLWrapper, JSON
sparql = SPARQLWrapper("http://purl.org/sudo/ontology/sudo.owl")
sparql.setQuery("""
SELECT ?subject
WHERE {?subject ?verb ?object}
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
print results.keys()
The above code works fine with a different ontology, which suggests that the ontology is the issue. I'm not sure what the issue with the ontology could be. I generated the ontology with Protege, it can load into vOWL, and it passes vOWL's ontology validation.
SPARQLWrapper()
's first argument should be a SPARQL endpoint address:
from rdflib import Graph
g = Graph()
g.parse("http://purl.org/sudo/ontology/sudo.owl", format="xml")
qres = g.query("""
SELECT DISTINCT ?s {
?s ?p ?o
}""")
for row in qres:
print("%s" % row)
If you really need SPARQL query results in JSON format (spec):
import sys
from rdflib import Graph
from rdflib.plugins.sparql.results.jsonresults import JSONResultSerializer
g = Graph()
g.parse("http://purl.org/sudo/ontology/sudo.owl", format="xml")
qres = g.query("""
SELECT DISTINCT ?s {
?s ?p ?o
}""")
JSONResultSerializer(qres).serialize(sys.stdout)
If you wish to abstract from RDF serialization, you should use owlready.