title: Correct way to write JSON data returned from a SPARQL query in Python
body: <p>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.</p>
  
  <p>Anyway, I can get the data out like this:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>When I do this, I get the following output in the file:</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>and so on.  I'm not sure what the <code>b</code> prefix does on these lines.  Anyway, I'm having trouble reading this in with the JSON libraries.  So I would <em>prefer</em> to write it with JSON.</p>
  
  <p>I would like to replace the for loop with a simple thing like </p>
  
  <pre><code>json.dump(jsonData, f)
  </code></pre>
  
  <p>or</p>
  
  <pre><code>json.dumps(jsonData, f)
  </code></pre>
  
  <p>When I do that I get the error message <code>Oops: &lt;class 'TypeError'&gt;</code>.
  I notice that the type of <code>jsonData</code> is <code>&lt;class 'SPARQLWrapper.Wrapper.QueryResult'&gt;</code>.</p>
  
  <p>Is the SPARQL query not returning JSON?  Is there some other transformation I have to make?</p>