title: How to use column names when creating JSON object, Python
body: <p>In order to improve readability in my code I want to use plain words instead index numbers, when creating JSON object:</p>
  
  <p>this is my database table school_subjects:</p>
  
  <pre><code>mysql&gt; DESCRIBE school_subjects;
  +------------------+--------------+------+-----+---------+----------------+
  | Field            | Type         | Null | Key | Default | Extra          |
  +------------------+--------------+------+-----+---------+----------------+
  | id               | int(11)      | NO   | PRI | NULL    | auto_increment |
  | name             | varchar(500) | NO   |     | NULL    |                |
  | user_id          | int(11)      | NO   | MUL | NULL    |                |
  | created_by       | varchar(64)  | NO   |     | NULL    |                |
  | created_time     | datetime     | NO   |     | NULL    |                |
  | num_of_followers | int(11)      | NO   |     | NULL    |                |
  +------------------+--------------+------+-----+---------+----------------+
  6 rows in set (0.00 sec)
  
  mysql&gt; 
  </code></pre>
  
  <p>my python code:</p>
  
  <pre><code>serg@serg-PORTEGE-Z835:~$ python
  Python 2.7.2+ (default, Oct  4 2011, 20:03:08) 
  [GCC 4.6.1] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import simplejson as json
  &gt;&gt;&gt; import MySQLdb
  &gt;&gt;&gt; import collections
  &gt;&gt;&gt; 
  &gt;&gt;&gt; mydb = MySQLdb.connect(host='localhost', user='root', passwd='', db='schooldb')
  &gt;&gt;&gt; cursor = mydb.cursor()
  &gt;&gt;&gt; cursor.execute("""
  ...                     SELECT id, name
  ...                     FROM school_subjects
  ...             """)
  6L
  &gt;&gt;&gt; rows = cursor.fetchall()
  &gt;&gt;&gt; result = []
  &gt;&gt;&gt; for row in rows:
  ...     d = dict()
  ...     d['id'] = row.id        #I want something similar to this
  ...     d['name'] = row.name    #but it doesn't work
  ...     result.append(d)
  ... 
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 2, in &lt;module&gt;
  AttributeError: 'tuple' object has no attribute 'id'
  </code></pre>
  
  <p>as you can see I'm getting this error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 2, in &lt;module&gt;
  AttributeError: 'tuple' object has no attribute 'id'
  </code></pre>
  
  <p>however this code works fine:</p>
  
  <pre><code>&gt;&gt;&gt; result = []
  &gt;&gt;&gt; for row in rows:
  ...     d = dict()
  ...     d['id'] = row[0]
  ...     d['name'] = row[1]
  ...     result.append(d)
  ... 
  &gt;&gt;&gt; subjects = json.dumps(result, indent=4)
  &gt;&gt;&gt; print subjects
  [
      {
          "id": 1,
          "name": "Math 140"
      },
      {
          "id": 2,
          "name": "English 102"
      },
      {
          "id": 3,
          "name": "CS 240"
      },
      {
          "id": 4,
          "name": "CS 210"
      },
      {
          "id": 5,
          "name": "Math 140"
      },
      {
          "id": 6,
          "name": "English 102"
      }
  ]
  &gt;&gt;&gt; 
  </code></pre>