title: Python convert JSON to CSV
body: <p>I have a JSON file which contains:</p>
  
  <pre><code>{
      "leaderboard": {
          "$": [
              {
                  "userId": 1432024286216,
                  "userName": "Joe Bloggs",
                  "score": 111111,
                  "gameType": "standard",
                  "dateCreated": 1432024397833,
                  "_id": 1432024397833
              },
              {
                  "userId": 1432024626556,
                  "userName": "Jane Bloggs",
                  "score": 222222,
                  "gameType": "demo",
                  "dateCreated": 1432024730861,
                  "_id": 1432024730861
              }
          ]
      },
      "users": {
          "$": [
              {
                  "userId_U": 1432024286000,
                  "userName_U": "Paul Bloggs",
                  "score_U": 333333,
                  "gameType_U": "standard",
                  "dateCreated_U": 1432024397833,
                  "_id_U": 1432024397833
              },
              {
                  "userId_U": 1432024626777,
                  "userName_U": "John Bloggs",
                  "score_U": 444444,
                  "gameType_U": "demo",
                  "dateCreated_U": 1432024730861,
                  "_id_U": 1432024730861
              }
          ]
      }
  }
  </code></pre>
  
  <p>I am trying to create a CSV from this in Python. The CSV creates the headers: userId, userName etc. only from the 'leaderboard' data object only and populate the corresponding data for it. So create a column each for: userId, userName etc.</p>
  
  <p>I started coding this but I am getting the 'leaderboard' and 'users' headers created and their data in one cell beneath them. My code:</p>
  
  <pre><code>import json, csv
  
  x = open('test/dbTest.json')
  
  rows = json.load(x)
  with open('test.csv', 'wb+') as f:
      dict_writer = csv.DictWriter(f, fieldnames=['leaderboard', 'users'])
      dict_writer.writeheader()
      dict_writer.writerow(rows)
  </code></pre>
  
  <p>I have tried to change the field name to 'userId' , 'userName' etc but it then gives error:</p>
  
  <blockquote>
    <p>ValueError: dict contains fields not in fieldnames: u'users', u'leaderboard'</p>
  </blockquote>
  
  <p>How can I extract the data I need? Why is the above code incorrect?</p>
  
  <p>Also, the CSV should look like:</p>
  
  <pre><code>userId,userName,score,gameType,dateCreated,_id,
  1432024286216,Joe Bloggs,111111,standard,1432024397833,1432024397833
  1432024626556,Jane Bloggs,222222,demo,1432024730861,1432024730861
  </code></pre>
  
  <p>and to clarify, 'users' and 'leaderboard' are different with different field names.</p>