I'm using python3 to make SPARQL queries. I need to read a Virtuoso database and output triples. Some of the data in the triples contains special characters like linefeeds and such.
Anyway, I can get the data out like this:
queryString = "some query"
sparql.setQuery(queryString)
sparql.setReturnFormat(JSON)
try:
jsonData = sparql.query()
for result in jsonData:
print('Result: ***')
f.write(str(result) + '\n')
except:
print("Oops:", sys.exc_info()[0], file=sys.stderr)
When I do this, I get the following output in the file:
b'{\n'
b' "head" : {\n'
b' "vars" : [\n'
b' "subject",\n'
b' "predicate",\n'
b' "object"\n'
b' ]\n'
b' },\n'
b' "results" : {\n'
b' "bindings" : [\n'
b' {\n'
b' "predicate" : {\n'
b' "type" : "uri",\n'
b' "value" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"\n'
b' },\n'
b' "subject" : {\n'
b' "type" : "uri",\n'
b' "value" : "http://www.ontologyrepository.com/CommonCoreOntologies/delimits"\n'
b' },\n'
b' "object" : {\n'
b' "type" : "uri",\n'
b' "value" : "http://www.w3.org/2002/07/owl#InverseFunctionalProperty"\n'
b' }\n'
b' },\n'
and so on. I'm not sure what the b prefix does on these lines. Anyway, I'm having trouble reading this in with the JSON libraries. So I would prefer to write it with JSON.
I would like to replace the for loop with a simple thing like
json.dump(jsonData, f)
or
json.dumps(jsonData, f)
When I do that I get the error message Oops: <class 'TypeError'>.
I notice that the type of jsonData is <class 'SPARQLWrapper.Wrapper.QueryResult'>.
Is the SPARQL query not returning JSON? Is there some other transformation I have to make?
b' '(i.e., it's not just abprefix) and the linefeeds within the JSON are being replaced with\n-- which I cannot immediately explain, but it seems that cleanup should be fairly easy with any number of tools, if we cannot figure out how to prevent these.define output:format "fmt"output pragma to start the query string, specifying "JSON", instead of Python'ssparql.setReturnFormat(JSON)