Iterating through json object in python
<p>I have a response from Foursquare that reads as follows</p>

<pre><code>response: {
    suggestedFilters: {},
    geocode: {},
    headerLocation: "Harlem",
    headerFullLocation: "Harlem",
    headerLocationGranularity: "city",
    query: "coffee",
    totalResults: 56,
    suggestedBounds: {},
    groups: [{
                type: "Recommended Places",
                name: "recommended",
                items: [{
                            reasons: {
                                count: 1,
                                items: [{
                                    summary: "You've been here 6 times",
                                    type: "social",
                                    reasonName: "friendAndSelfCheckinReason",
                                    count: 0
                                }]
                            },
                            venue: {
                                id: "4fdf5edce4b08aca4a462878",
                                name: "The Chipped Cup",
                                contact: {},
                                location: {},
                                categories: [],
                                verified: true,
                                stats: {},
                                url: "http://www.chippedcupcoffee.com",
                                price: {},
                                hasMenu: true,
                                rating: 8.9,
                                ratingColor: "73CF42",
                                ratingSignals: 274,
                                menu: {},
                                allowMenuUrlEdit: true,
                                beenHere: {},
                                hours: {},
                                photos: {},
                                venuePage: {},
                                storeId: "",
                                hereNow: {}
                            },
                            tips: [],
                            referralId: "e-0-4fdf5edce4b08aca4a462878-0"
                        },

]
</code></pre>

<p>If I type the following: </p>

<pre><code>for value in json_data['response']['groups'][0]:
print(value['name'])
</code></pre>

<p>I get a TypeError: string indices must be integers</p>

<p>I'm just wondering how to iterate through this response to get the names of businesses. </p>

<p>Thanks</p>

<p>You went too far. The [0] is the first element of the groups</p>

<pre><code>for value in json_data['response']['groups']
</code></pre>

<p>Or you need to actually parse the JSON data from a string with the <code>json.loads</code> function </p>

<p>Also, I think you want </p>

<pre><code>value['venue']['name']
</code></pre>

python json