1

I have loaded the following json this way:

url2 = "https://gbfs.capitalbikeshare.com/gbfs/en/station_status.json"
response2 = urllib2.urlopen(url2)
cabi_station_status = json.load(response2)

sample output of cabi_station_status:

{u'stations': [{u'eightd_has_available_keys': False,
   u'is_installed': 1,
   u'is_renting': 1,
   u'is_returning': 1,
   u'last_reported': 1489309320,
   u'num_bikes_available': 5,
   u'num_bikes_disabled': 0,
   u'num_docks_available': 10,
   u'num_docks_disabled': 0,
   u'station_id': u'1'},
  {u'eightd_has_available_keys': False,
   u'is_installed': 1,
   u'is_renting': 1,
   u'is_returning': 1,
   u'last_reported': 1489309256,
   u'num_bikes_available': 5,
   u'num_bikes_disabled': 0,
   u'num_docks_available': 6,
   u'num_docks_disabled': 0,
   u'station_id': u'2'}

I cannot figure out how to access the value of "num_bikes_available" of certain stations. When I try cabi_station_status["stations"]["station_id"][1]

it does not return anything. Pseudo code for my ideal output would be to get the "num_bikes"available" when "station_id" == 1. and return an integer, in this case, 5.

3 Answers 3

1

cabi_station_status["stations"] is a list.

You should use a loop to iterate over each station and from each station get num_bikes_available.

each_station['num_bikes_available']

Your solution would look like:

for each_station in cabi_station_status["stations"]:
    if each_station['station_id'] == '1':
        return each_station['num_bikes_available']
Sign up to request clarification or add additional context in comments.

Comments

0
for station in cabi_station_status['stations']:
    if station['station_id'] == '1':
        print(station['num_bikes_available'])

1 Comment

That worked! I was so close with my attempt: 'for key in cabi_station_status(): ] if cabi_station_status.get("station_id","") == 1: print ["num_bikes_available"'
0

cabi_station_status["stations"] is a list; you need to refer to its elements by index, not key.

cabi_station_status["stations"][0]["num_bikes_available"]

1 Comment

hmm, so no way to selected num_bikes_available based off of a station_id? The index is not 1:1 for the stations, ie index 12 = station_id:14 and index 45 = station_id: 52

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.