title: How to import data from nested JSON file to CSV?
body: <p>I have a nested json file and I want to write some of its contents into a csv file using Python. I have managed to select the data I want, but I don't understand how to ask Python to write data from all the indexes. In other words, I can only write data from one index at a time, I don't understand how to select all indexes. I tried with a slice but it gives me TypeError (list indices must be integers or slices, etc.). Could you help me? Thanks!</p>
  <p>This is the code so far (it works, but it just writes 1 line and I have 87):</p>
  <pre><code>import json
  import csv
  
  
  json_file = open(&quot;unique_json.json&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;)
  csv_file = open(&quot;csv_format2.csv&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;)
  
  data = json.load(json_file)
  write = csv.writer(csv_file)
  
  
  contents = [[data[0][&quot;node&quot;][&quot;display_url&quot;]],
              [data[0][&quot;node&quot;][&quot;edge_liked_by&quot;][&quot;count&quot;]],
              [data[0][&quot;node&quot;][&quot;edge_media_to_comment&quot;][&quot;count&quot;]],
              [data[0][&quot;node&quot;][&quot;taken_at_timestamp&quot;]]]
  
  headers = [&quot;url&quot;, &quot;likes&quot;, &quot;comments&quot;, &quot;timestamp&quot;]
  
  write.writerow(headers)
  
  for i in contents:
      for j in i:
          print(j, end=&quot;,&quot;, file=csv_file)
  
  json_file.close()
  csv_file.close()
  </code></pre>