title: Convert mySql query result to json using python
body: <p>I want to monitor mySql Database for getting update of it's performance so I executed a query <code>show global status;</code> This gives me a list of varibale like <code>Uptime, aborted_connections</code> etc.</p>
  
  <p>But I want to export the result in a json format, can I achieve that using python script?</p>
  
  <p>What I have done till now is, </p>
  
  <ol>
  <li>exported result of query to .csv file .</li>
  <li>Trying to convert that csv file to json using the following python code</li>
  </ol>
  
  <blockquote>
  <pre><code> import csv
  import json
  csvfile = open ('./tempp/op.csv','r')
  jsonfile = open('./tempp/op.json','w')
  fieldnames = ("Variable_name","Value")
  reader = csv.DictReader(csvfile,fieldnames)
  for row in reader:
    json.dump(row,jsonfile)
    jsonfile.write('\n')
  </code></pre>
  </blockquote>
  
  <p>Issues:</p>
  
  <ol>
  <li>The above script is not giving the desired result.It produces json like</li>
  </ol>
  
  <blockquote>
  <pre><code>{"Value": null, "Variable_name": "Variable_name\tValue"}
  {"Value": null, "Variable_name": "Aborted_clients\t0"}
  {"Value": null, "Variable_name": "Aborted_connects\t7"}
  {"Value": null, "Variable_name": "Binlog_cache_disk_use\t0"}
  {"Value": null, "Variable_name": "Binlog_cache_use\t0"}
  {"Value": null, "Variable_name": "Binlog_stmt_cache_disk_use\t0"}
  {"Value": null, "Variable_name": "Binlog_stmt_cache_use\t0"}
  </code></pre>
  </blockquote>
  
  <ol start="2">
  <li>This seems to be a bad approach to first write the result to a file and then read data from it. Is there any better approach to directly convert result from mySql query to json object directly?</li>
  </ol>
  
  <p><code>Edit</code> based on answers:
  My op.csv file looks like:</p>
  
  <pre><code>Variable_name   Value
  Aborted_clients 0
  Aborted_connects    7
  Binlog_cache_disk_use   0
  Binlog_cache_use    0
  Binlog_stmt_cache_disk_use  0
  Binlog_stmt_cache_use   0
  Bytes_received  31075
  Bytes_sent  1891186
  Com_admin_commands  445
  Com_assign_to_keycache  0
  Com_alter_db    0
  Com_alter_db_upgrade    0
  </code></pre>