Search code examples
pythonjsonsparqlsparqlwrapper

Correct way to write JSON data returned from a SPARQL query in Python


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?


Solution

  • The b in front of your String means that your String is not an string but an byte string and your bytes are interpreted as chars for printing. Look here for more information

    For the next time it would be easier if you tell us more of your problem. What library you are using, which version, example query etc.

    In your case i assume your are using SPARQLWrapper Library. You are very close to solve your problem, you only need to convert your QueryResult. Just call jsonData = sparql.query().convert() instead of jsonData = sparql.query() and you will get an dict which can be written to a json file with json.dump.