0

I have a response that I receive from foursquare in the form of json. I have tried to access the certain parts of the object but have had no success. How would I access say the address of the object? Here is my code that I have tried.

url = 'https://api.foursquare.com/v2/venues/explore'

params = dict(client_id=foursquare_client_id,
              client_secret=foursquare_client_secret,
              v='20170801', ll=''+lat+','+long+'',
    query=mealType, limit=100)

resp = requests.get(url=url, params=params)

data = json.loads(resp.text)
msg = '{} {}'.format("Restaurant Address: ", 
data['response']['groups'][0]['items'][0]['venue']['location']['address'])
print(msg)

Here is an example of json response:

"items": [
      {
        "reasons": {
          "count": 0,
          "items": [
            {
              "summary": "This spot is popular",
              "type": "general",
              "reasonName": "globalInteractionReason"
            }
          ]
        },
        "venue": {
          "id": "412d2800f964a520df0c1fe3",
          "name": "Central Park",
          "contact": {
            "phone": "2123106600",
            "formattedPhone": "(212) 310-6600",
            "twitter": "centralparknyc",
            "instagram": "centralparknyc",
            "facebook": "37965424481",
            "facebookUsername": "centralparknyc",
            "facebookName": "Central Park"
          },
          "location": {
            "address": "59th St to 110th St",
            "crossStreet": "5th Ave to Central Park West",
            "lat": 40.78408342593807,
            "lng": -73.96485328674316,
            "labeledLatLngs": [
              {
                "label": "display",
                "lat": 40.78408342593807,
                "lng": -73.96485328674316
              }
            ],

the full response can be found here

2
  • Including what happens when you run the code will improve the quality and specificity of the answers you receive. After you updated your question, assuming that you correctly import the requisite modules and provide the foursquare credentials, your code should work as is now. If it's not, then sharing the error or errant behavior you see should help make the issue immediately apparent. Commented Jan 31, 2018 at 20:31
  • Almost duplicate of Python Accessing Nested JSON Data - Stack Overflow -- although in this case the issue is a typo rather than the OP not knowing how to do it. Commented Feb 18, 2021 at 3:39

4 Answers 4

1

Like so

addrs=data['items'][2]['location']['address']
Sign up to request clarification or add additional context in comments.

Comments

1

Your code (at least as far as loading and accessing the object) looks correct to me. I loaded the json from a file (since I don't have your foursquare id) and it worked fine. You are correctly using object/dictionary keys and array positions to navigate to what you want. However, you mispelled "address" in the line where you drill down to the data. Adding the missing 'a' made it work. I'm also correcting the typo in the URL you posted.

I answered this assuming that the example JSON you linked to is what is stored in data. If that isn't the case, a relatively easy way to see exact what python has stored in data is to import pprint and use it like so: pprint.pprint(data).

You could also start an interactive python shell by running the program with the -i switch and examine the variable yourself.

Comments

0

data["items"][2]["location"]["address"]

This will access the address for you.

Comments

0

You can go to any level of nesting by using integer index in case of an array and string index in case of a dict. Like in your case items is an array

#items[int index]
items[0]

Now items[0] is a dictionary so we access by string indexes

item[0]['location']

Now again its an object s we use string index

item[0]['location']['address]

Comments

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.