0
import requests

url = "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo"
request = requests.get(url)
fetch_data = request.json()

print (request.content)

for item in fetch_data:
    print(item)

I am trying to fetch items within JSON Request geonames -> lng. If I make use of item['lng']

Then getting error

TypeError: string indices must be integers

EXAMPLE of JSON Request

{  
   "geonames":[  
      {  
         "lng":-99.12766456604,
         "geonameId":3530597,
         "countrycode":"MX",
         "name":"Mexiko-Stadt",
         "fclName":"city, village,...",
         "toponymName":"Mexico City",
         "fcodeName":"capital of a political entity",
         "wikipedia":"en.wikipedia.org/wiki/Mexico_City",
         "lat":19.428472427036,
         "fcl":"P",
         "population":12294193,
         "fcode":"PPLC"
      }
    ]
}
4
  • You're iterating over the dictionary fetch_data, so item is the key (e.g. 'geonames'), which is a string, as the error message tells you. Commented Feb 17, 2017 at 23:29
  • Use for key, item in fetch_data.enumerate(): Commented Feb 17, 2017 at 23:34
  • 1
    @Barmar 'dict' object has no attribute 'enumerate'...? Commented Feb 17, 2017 at 23:35
  • This code doesn't run... the daily limit of 30000 credits for demo has been exceeded. Commented Feb 17, 2017 at 23:37

1 Answer 1

2

Try;

for item in fetch_data['geonames']:
    print(item['lng'])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.