2

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?

3
  • I'm guessing you're using some Python library, not strict Python. Specific version details (Python, that library, target Virtuoso) may matter. For some reason, each line of the output file is being wrapped in b' ' (i.e., it's not just a b prefix) 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. Commented Jul 23, 2018 at 16:55
  • You might try using Virtuoso's define output:format "fmt" output pragma to start the query string, specifying "JSON", instead of Python's sparql.setReturnFormat(JSON) Commented Jul 23, 2018 at 17:03
  • My computer system is going down until possibly Wednesday, but I'll try the output:format first thing. Commented Jul 23, 2018 at 19:24

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.