0

I have a response from Foursquare that reads as follows

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"
                        },

]

If I type the following:

for value in json_data['response']['groups'][0]:
print(value['name'])

I get a TypeError: string indices must be integers

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

Thanks

2 Answers2

1

You went too far. The [0] is the first element of the groups

for value in json_data['response']['groups']

Or you need to actually parse the JSON data from a string with the json.loads function

Also, I think you want

value['venue']['name']
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I'm using the requests library which has a build in json function that parses the response I think? Also when I do as you suggested I get a new error: UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 11003: ordinal not in range(128) – Ron Arevalo Oct 14 '16 at 13:48
  • That error seems to point at an encoding problem, not a problem with my answer. If you update your question with a [mcve] of the requests code, then I'll look into it – OneCricketeer Oct 14 '16 at 13:54
  • @RonArevalo that error would also seem to suggest that you use Python 2 (yet parenthesize `print` like in Python 3); perhaps time to switch to Python 3 as well. – Antti Haapala -- Слава Україні Oct 16 '16 at 07:20
  • @AnttiHaapala Well, the question doesn't contain valid JSON (keys aren't quoted), so maybe that's the error. Hard to say – OneCricketeer Oct 16 '16 at 07:25
1

json_data['response']['groups'][0] is a dictionary. When you iterate over a dictionary, you are iterating over a list of keys, all of which are strings...so within the loop, value is a string.

So when you ask for value['name'], you are trying to index the string with ['name'], which doesn't make any sense, hence the error.

I think you meant:

for value in json_date['response']['groups']
larsks
  • 277,717
  • 41
  • 399
  • 399