Retrieve geographic information via an API with python

Viewed 42

I am a beginner in python. I'm trying to create a dictionary in a JSON file that I created before. This dictionary must contain the geometrical TYPE of the elements that I retrieve via an API. I tried with the following code but I have the following error message:

with open(filename) as json_file:
    data_raw = json.load(json_file) 
data_events = dict(type=data_raw['type'], features=[])

The API looks like this :

...
"geometry":{
    "type":"Point",
    "coordinates":[
          2.900875,
          48.550178
    ]
 },
...

the error I have :

Traceback (most recent call last):
File "<string>", line 25, in <module>
KeyError: 'type'

What should i do?

1 Answers

Notice that the object that you're attempting to access is assigned to the geometry field. These objects are in a list assigned to the records field in the outermost object. So you'll need to use data_raw["records"][(integer index of record you want to access)]["geometry"]["type"] to access the desired field.

Related