I have a geoJSON file, and I'd like to extract all the possible values in a subfield. So for a two items long json it would be like this:
data['features'][0]['properties']['cellId']
#returns 38
data['features'][1]['properties']['cellId']
#returns 51
I'd like to return [38, 51]
. Is it possible? I tried
data['features'][0:]['properties']['cellId']
but it doesn't work, since TypeError: list indices must be integers or slices, not str
Use a for
loop:
for element in data['features']:
print(element['properties']['cellId'])
Or use list comprehension if you want to store these instead of just printing them individually:
cell_ids = [element['properties']['cellId'] for element in data['features']]
print(cell_ids)
# [38, 51]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With