title: Type error : list indices must be integers or slices, not dict
body: <p>I am trying to read json data and writing to csv file but not working. How to approach for it to get my expected output</p>
  <p><strong>Code :</strong></p>
  <pre><code>import json
  import csv 
  import ast
  
  
  # Using json loads to load data from varaible which holds json data 
  jdata = json.loads(data)
  
  # Fetching all headers from json and converting into list   
  jkey = list(jdata[0].keys())  
  
  # get length 
  glen_count = len(data)
  
  
  # Looping through the keys to write data to csv file  
  for r in range(glen_count):
    for hd in jkey:
      with open(&quot;/content/sample_data/abc.csv&quot;,'w') as wr:
        csv_wr = csv.DictWriter(wr,fieldnames=jkey)
        csv_wr.writeheader()
        csv_wr.writerows(jdata[jdata[r][hd]])  # error : Type error : list indices must be integers or slices, not dict
  </code></pre>
  <p><em>My variable <code>data</code> contain json</em></p>
  <pre><code>data = '''
  [
  
   {
    &quot;_id&quot;: {
      &quot;$oid&quot;: &quot;5fc7897969b8ab14e1cad8f4&quot;
    },
    &quot;m&quot;: &quot;24:6F:28:55:7C:55&quot;,
    &quot;t&quot;: &quot;o&quot;,
    &quot;pId&quot;: {
      &quot;$oid&quot;: &quot;5fc7894ff7fa2b262ecb6c46&quot;
    },
    &quot;pVal&quot;: {
      &quot;title&quot;: &quot;Switch&quot;,
      &quot;val&quot;: 1,
      &quot;status&quot;: null,
      &quot;portType&quot;: &quot;AC&quot;,
      &quot;portIcon&quot;: &quot;AC&quot;,
      &quot;_id&quot;: &quot;5fc7894ff7fa2b262ecb6c46&quot;,
      &quot;no&quot;: &quot;0&quot;,
      &quot;cat&quot;: &quot;s&quot;,
      &quot;device&quot;: {
        &quot;ports&quot;: [
          &quot;5fc7894ff7fa2b262ecb6c46&quot;
        ],
        &quot;_id&quot;: &quot;5fc7894ff7fa2b262ecb6c45&quot;,
        &quot;macId&quot;: &quot;24:6F:28:55:7C:55&quot;,
        &quot;deviceId&quot;: &quot;LTAC21220050009&quot;,
        &quot;dType&quot;: &quot;5ee38d3a4afdf74cf8b07dfb&quot;,
        &quot;createdAt&quot;: &quot;2020-12-02T12:32:15.235Z&quot;,
        &quot;updatedAt&quot;: &quot;2020-12-02T12:32:36.773Z&quot;,
        &quot;__v&quot;: 0,
        &quot;assignedTo&quot;: &quot;5fc62223f7fa2b262ecb6c2d&quot;
      },
      &quot;mac&quot;: &quot;24:6F:28:55:7C:55&quot;,
      &quot;__v&quot;: 0,
      &quot;createdAt&quot;: &quot;2020-12-02T12:32:15.237Z&quot;,
      &quot;updatedAt&quot;: &quot;2020-12-02T12:32:57.776Z&quot;
    },
    &quot;dId&quot;: {
      &quot;$oid&quot;: &quot;5fc7894ff7fa2b262ecb6c45&quot;
    },
    &quot;createdAt&quot;: {
      &quot;$date&quot;: &quot;2020-12-02T12:32:57.781Z&quot;
    },
    &quot;updatedAt&quot;: {
      &quot;$date&quot;: &quot;2020-12-02T12:32:57.781Z&quot;
    },
    &quot;__v&quot;: 0
  },{
    &quot;_id&quot;: {
      &quot;$oid&quot;: &quot;5fc7897b69b8ab14e1cad8f5&quot;
    },
    &quot;m&quot;: &quot;24:6F:28:55:7C:55&quot;,
    &quot;t&quot;: &quot;o&quot;,
    &quot;pId&quot;: {
      &quot;$oid&quot;: &quot;5fc7894ff7fa2b262ecb6c46&quot;
    },
    &quot;pVal&quot;: {
      &quot;title&quot;: &quot;Switch&quot;,
      &quot;val&quot;: 0,
      &quot;status&quot;: null,
      &quot;portType&quot;: &quot;AC&quot;,
      &quot;portIcon&quot;: &quot;AC&quot;,
      &quot;_id&quot;: &quot;5fc7894ff7fa2b262ecb6c46&quot;,
      &quot;no&quot;: &quot;0&quot;,
      &quot;cat&quot;: &quot;s&quot;,
      &quot;device&quot;: {
        &quot;ports&quot;: [
          &quot;5fc7894ff7fa2b262ecb6c46&quot;
        ],
        &quot;_id&quot;: &quot;5fc7894ff7fa2b262ecb6c45&quot;,
        &quot;macId&quot;: &quot;24:6F:28:55:7C:55&quot;,
        &quot;deviceId&quot;: &quot;LTAC21220050009&quot;,
        &quot;dType&quot;: &quot;5ee38d3a4afdf74cf8b07dfb&quot;,
        &quot;createdAt&quot;: &quot;2020-12-02T12:32:15.235Z&quot;,
        &quot;updatedAt&quot;: &quot;2020-12-02T12:32:36.773Z&quot;,
        &quot;__v&quot;: 0,
        &quot;assignedTo&quot;: &quot;5fc62223f7fa2b262ecb6c2d&quot;
      },
      &quot;mac&quot;: &quot;24:6F:28:55:7C:55&quot;,
      &quot;__v&quot;: 0,
      &quot;createdAt&quot;: &quot;2020-12-02T12:32:15.237Z&quot;,
      &quot;updatedAt&quot;: &quot;2020-12-02T12:32:59.951Z&quot;
    },
    &quot;dId&quot;: {
      &quot;$oid&quot;: &quot;5fc7894ff7fa2b262ecb6c45&quot;
    },
    &quot;createdAt&quot;: {
      &quot;$date&quot;: &quot;2020-12-02T12:32:59.955Z&quot;
    },
    &quot;updatedAt&quot;: {
      &quot;$date&quot;: &quot;2020-12-02T12:32:59.955Z&quot;
    },
    &quot;__v&quot;: 0
  }
  
  ]
  </code></pre>
  <p>Expected output is need to write to csv file : column : <code>id , val , createdAt , updatedAt</code></p>
  <pre><code>id , val , createdAt , updatedAt
  5fc7897969b8ab14e1cad8f4 , 0 , 2020-12-02T12:32:57.781Z , 2020-12-02T12:32:57.781Z
  5fc7897b69b8ab14e1cad8f5 , 0 , 2020-12-02T12:32:59.955Z , 2020-12-02T12:32:59.955Z
  </code></pre>