title: How to Iterate through nested JSON in Python
body: <p>I would like to find out how I can iterate through the Following JSON in Python 3  Specifically I would like to be able to pull out 'id' and 'lname'. My actual JSON file has about 300 entries</p>
  
  <pre><code>{
  "datainfos": [{
          "DataInfo": [{
              "id": 1,
              "lname": "Altitude",
              "max": 62.79999923706055,
              "min": -37.20000076293945,
              "numInstances": 1,
              "sname": "ALT",
              "unit": "m"
          }]
      },
      {
          "DataInfo": []
      },
      {
          "DataInfo": [{
              "id": 3,
              "lname": "Position Error",
              "max": 0,
              "min": 0,
              "numInstances": 1,
              "sname": "EPE",
              "unit": "m"
          }]
      },
      {
          "DataInfo": [{
              "id": 4,
              "lname": "HDOP",
              "max": 0,
              "min": 0,
              "numInstances": 1,
              "sname": "HDOP",
              "unit": ""
          }]
      }
  ]
  }
  </code></pre>
  
  <p>My code is as below:</p>
  
  <pre><code>import json
  
  f = open('data1.json')
  data = json.load(f)
  f.close()
  
  
  for dataitems in data['datainfos']:
      print (dataitems['DataInfo'])
  </code></pre>
  
  <p>Python returns a list, not a dictionary. When I try to use the following code </p>
  
  <pre><code>import json
  
  f = open('data1.json')
  data = json.load(f)
  f.close()
  
  
  for dataitems in data['datainfos']:
      print (dataitems['DataInfo']['lnames'])
  </code></pre>
  
  <p>I get the error:</p>
  
  <blockquote>
    <p>Traceback (most recent call last):
          File "C:\Users\phil\Documents\Python Scripts\UMP-VDR\Testing Files\convertjson.py", line 9, in 
            print (dataitems['DataInfo']['lnames'])
        TypeError: list indices must be integers or slices, not str
        [Finished in 0.1s]</p>
  </blockquote>