Unable to loop through JSON output from webservice Python
Question: I have a web-service call (HTTP Get) that my Python script makes in which
returns a JSON response. The response looks to be a list of Dictionaries. The
script's purpose is to iterate through the each dictionary, extract each piece
of metadata (i.e. "ClosePrice": "57.74",) and write each dictionary to its own
row in Mssql.

The issue is, I don't think Python is recognizing the JSON output from the API
call as a list of dictionaries, and when I try a for loop, I'm getting the
error `must be int not str`. I have tried converting the output to a list,
dictionary, tuple. I've also tried to make it work with List Comprehension,
with no luck. Further, if I copy/paste the data from the API call and assign
it to a variable, it recognizes that its a list of dictionaries without issue.
Any help would be appreciated. I'm using Python 2.7.

Here is the actual http call being made:
[http://test.kingegi.com/Api/QuerySystem/GetvalidatedForecasts?user=kingegi&market=us&startdate=08/19/13&enddate=09/12/13](http://test.kingegi.com/Api/QuerySystem/GetvalidatedForecasts?user=kingegi&market=us&startdate=08/19/13&enddate=09/12/13)

Here is an abbreviated JSON output from the API call:

    
    
    [
    
    {
          "Id": "521d992cb031e30afcb45c6c",
          "User": "kingegi",
          "Symbol": "psx",
          "Company": "phillips 66",
          "MarketCap": "34.89B",
          "MCapCategory": "large",
          "Sector": "basic materials",
          "Movement": "up",
          "TimeOfDay": "close",
          "PredictionDate": "2013-08-29T00:00:00Z",
          "Percentage": ".2-.9%",
          "Latency": 37.48089483333333,
          "PickPosition": 2,
          "CurrentPrice": "57.10",
          "ClosePrice": "57.74",
          "HighPrice": null,
          "LowPrice": null,
          "Correct": "FALSE",
          "GainedPercentage": 0,
          "TimeStamp": "2013-08-28T02:31:08 778",
          "ResponseMsg": "",
          "Exchange": "NYSE  "
       },
    
    {
          "Id": "521d992db031e30afcb45c71",
          "User": "kingegi",
          "Symbol": "psx",
          "Company": "phillips 66",
          "MarketCap": "34.89B",
          "MCapCategory": "large",
          "Sector": "basic materials",
          "Movement": "down",
          "TimeOfDay": "close",
          "PredictionDate": "2013-08-29T00:00:00Z",
          "Percentage": "16-30%",
          "Latency": 37.4807215,
          "PickPosition": 1,
          "CurrentPrice": "57.10",
          "ClosePrice": "57.74",
          "HighPrice": null,
          "LowPrice": null,
          "Correct": "FALSE",
          "GainedPercentage": 0,
          "TimeStamp": "2013-08-28T02:31:09 402",
          "ResponseMsg": "",
          "Exchange": "NYSE  "
       }
    
    ]
    
    Small Part of code being used:
    
    import os,sys
    import subprocess
    import glob
    from os import path
    import urllib2
    import json
    import time
    try:
        data = urllib2.urlopen('http://api.kingegi.com/Api/QuerySystem/GetvalidatedForecasts?user=kingegi&market=us&startdate=08/10/13&enddate=09/12/13').read()
    except urllib2.HTTPError, e:
        print "HTTP error: %d" % e.code
    except urllib2.URLError, e:
        print "Network error: %s" % e.reason.args[1]
    list_id=[x['Id'] for x in data] #test to see if it extracts the ID from each Dict
    print(data) #Json output
    print(len(data)) #should retrieve the number of dict in list
    


Answer: **UPDATE**

Answered my own question, here is the method below:

    
    
    `url = 'some url that is a list of dictionaries' #GetCall
    u = urllib.urlopen(url) # u is a file-like object
    data = u.read()
    newdata = json.loads(data)
    print(type(newdata)) # printed data type will show as a list
    print(len(newdata)) #the length of the list
    newdict = newdata[1] # each element in the list is a dict
    print(type(newdict)) # this element is a dict
    length = len(newdata) # how many elements in the list
    for a in range(1,length): #a is a variable that increments itself from 1 until a number
        var = (newdata[a])
        print(var['Correct'], var['User'])`
    

