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

3
  • Possible duplicate of Iterating through a JSON object Commented Oct 14, 2016 at 13:35
  • 1
    It clearly doesnt look like valid JSON Commented Oct 14, 2016 at 13:37
  • You're missing a ] and a } Commented Oct 14, 2016 at 15:00

2 Answers 2

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']
Sign up to request clarification or add additional context in comments.

Comments

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']

4 Comments

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)
That error seems to point at an encoding problem, not a problem with my answer. If you update your question with a minimal reproducible example of the requests code, then I'll look into it
@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.
@AnttiHaapala Well, the question doesn't contain valid JSON (keys aren't quoted), so maybe that's the error. Hard to say

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.