title: Send JSON response from Sqlite queries in Python
body: <p>I have a Python http server which listens to JSON based requests. After receiving the request, it parses the key from the JSON input, and queries Sqlite database which has such a key. Now I want to respond the request with a result JSON message. I am new to Python, and I don't know how. </p>
  
  <p>My code structure is like below:</p>
  
  <pre><code> import ...
  
   key=...;//get key from request
   con = lite.connect('test.db')
   with con:
      con.row_factory = lite.Row
      cur = con.cursor()
      cur.execute("SELECT * FROM mytable ");
      while True:
  
          row = cur.fetchone()
  
          if row == None:
              break
          if key==row['key']:
              # How can I add the record to the response?
  </code></pre>
  
  <p>And the handler will write the response like this (in a class inherit BaseHTTPRequestHandler and started by a thread)</p>
  
  <pre><code>self.send_response(200)
  self.send_header('Content-Type', 'application/json')
  self.end_headers()
  self.wfile.write(??????) # What do I need to write here?
  </code></pre>