title: Convert JSON file to CSV file with Python
body: <p>I have the following JSON file:</p>
  
  <pre><code>    {
      "took": 17,
      "timed_out": false,
      "_shards": {
          "total": 25,
          "successful": 25,
          "skipped": 0,
          "failed": 0
      },
      "hits": {
          "total": 3,
          "max_score": 1,
          "hits": [
              {
                  "_index": "w106941",
                  "_type": "wqr",
                  "_id": "51929",
                  "_score": 1,
                  "_source": {
                      "tstamp": "1978-12-06T23:00:00Z",
                      "value": 41,
                      "name": "SO4-Dis",
                  }
              },
              {
                  "_index": "w6406931",
                  "_type": "wqr",
                  "_id": "51929",
                  "_score": 1,
                  "_source": {
                      "tstamp": "1979-12-11T23:00:00Z",
                      "value": 45,
                      "name": "SO4",
                  }
              },
              {
                  "_index": "w2049185",
                  "_type": "wqr",
                  "_id": "51929",
                  "_score": 1,
                  "_source": {
                      "tstamp": "1980-12-22T23:00:00Z",
                      "value": 48,
                      "name": "T-D2",
                  }
              }
          ]
      }
  }
  </code></pre>
  
  <p>I want to create a <code>csv</code> with the following format in <strong>python</strong>:</p>
  
  <pre><code>tstamp;value;name;
  
  1978-12-06T23:00:00Z;41;SO4-Dis;
  
  1979-12-11T23:00:00Z;45;SO4;
  
  1980-12-22T23:00:00Z;48;T-D2;
  </code></pre>
  
  <p>I tried all codes in this page.
  Also I tried with <code>pandas df</code>.</p>
  
  <pre><code>df = pd.read_json("example.json")
  df_hits=df['hits']
  #df = df.loc[["sta_no_s", "wqtr_tstamp_ts", "part_shortname_s"]]
  df_hits.to_csv("sample.csv", index=False, sep=';')
  f = csv.writer(open("test.csv", "w+"))
  
  # Write CSV Header, If you dont need that, remove this line
  f.writerow(["tstamp", "value", "name"])
  
  
  for x in df:
      f.writerow([df["hits"]["tstamp"],
              df["hits"]["value"],
              df["hits"]["name"]])
  </code></pre>