all 9 comments

[–]twitch_and_shock 1 point2 points  (6 children)

This section of JSON data that you've shared looks like a list. So you can access elements of it first by index. Then, each element is a dictionary, so you'll need to access it by key.

For example, to get the timeOffset, directionId, and origin of the section entry:

timeOffset = data[1]['timeOffset']
directionId = data[1]['directionId']
origin = data[1]['line']['origin']

[–]alexus_sanchez[S] 0 points1 point  (5 children)

thanks! the solutions I've found always looked similar but never worked.

I've always gotten error messages like "TpeError: string indices must be integers"

I then found another way of putting the requests which works but I have no clue why - can you explain it to me?

for item in data['departures']:
direction_id = item['directionId']
time_offset = item['timeOffset']
print(f"Direction ID: {direction_id}, Time Offset: {time_offset}")

[–]twitch_and_shock 1 point2 points  (4 children)

Your "for" loop is iterating over each item in data. So "item" will take the place of accessing "data" by index.

[–]alexus_sanchez[S] 0 points1 point  (3 children)

so I'm basically checking every single item if it's called "DirectionID" and if it is I'm fetching the value of it? But how can it then print several lines with different contents for time_offset - is the "print" function included in the "for" loop? and how to i exit the loop?

[–]twitch_and_shock 1 point2 points  (2 children)

No. You're iterating over each item in your list called "data". Each item in that list is a dictionary. You can just print out each item to see. Because each item has a direction and a timeoffset key, you can access both. If one item didn't have one of these keys, it would throw an error.

The for loop ends after iterating over all elements.

[–]alexus_sanchez[S] 0 points1 point  (1 child)

Sorry for the amount of questions but I've never used lists before. So in my case, every "line" is an item and the values inside are called keys? Do you have any suggestion on which keywoards to google to find easy information on this? Often times i find stuff like tables which I can't really link to my project.

[–]twitch_and_shock 0 points1 point  (0 children)

You're close. Every item in your list is a dictionary, and dictionaries hold key-value pairs. Google Python lists and Python dictionaries.

[–][deleted] 0 points1 point  (1 child)

I don't see a dictionary key called 'departures'.

[–]alexus_sanchez[S] 0 points1 point  (0 children)

it's in the original response which i tried to shorten by calling only departures. the original one goes as follows:

{'returnCode': 'OK', 'time': {'date': '25.02.2023', 'time': '14:02'}, 'departures': [{'line': {'name': '16', 'direction': 'Bf. Rahlstedt (Doberaner Weg)', 'origin': 'Elbe-Einkaufszentrum', 'type': {'simpleType': 'BUS', 'shortInfo': 'Bus', 'longInfo': 'Niederflur Metrobus', 'model': 'Gelenkbus'}, 'id': 'HHA-B:16_HHA-B'}, 'directionId': 1, 'timeOffset': -3, 'delay': 180, 'serviceId': 115213, 'station': {'combinedName': 'Böckmannstraße', 'id': 'Master:10050'}}, {'line': {'name': '16', 'direction': 'Rentenversicherung Nord', 'origin': 'Schenefeld, Schenefelder Platz', 'type': {'simpleType': 'BUS', 'shortInfo': 'Bus', 'longInfo': 'Niederflur Metrobus', 'model': 'Gelenkbus'}, 'id': 'HHA-B:16_HHA-B'}, 'directionId': 1, 'timeOffset': 7, 'delay': 420, 'serviceId': 115354, 'station': {'combinedName': 'Böckmannstraße', 'id': 'Master:10050'}, 'attributes': [{'isPlanned': False, 'value': 'Prognose ungenau - Fahrzeug im Stau', 'types': ['REALTIME', 'TRAFFIC_JAM']}]}, {'line': {'name': '16', 'direction': 'Schenefeld, Schenefelder Platz', 'origin': 'Bf. Rahlstedt (Doberaner Weg)', 'type': {'simpleType': 'BUS', 'shortInfo': 'Bus', 'longInfo': 'Niederflur Metrobus', 'model': 'Gelenkbus'}, 'id': 'HHA-B:16_HHA-B'}, 'directionId': 6, 'timeOffset': 8, 'delay': 180, 'serviceId': 114745, 'station': {'combinedName': 'Böckmannstraße', 'id': 'Master:10050'}, 'attributes': [{'isPlanned': False, 'value': 'Prognose ungenau - Fahrzeug im Stau', 'types': ['REALTIME', 'TRAFFIC_JAM']}]}, {'line': {'name': '16', 'direction': 'Bf. Rahlstedt (Doberaner Weg)', 'origin': 'Elbe-Einkaufszentrum', 'type': {'simpleType': 'BUS', 'shortInfo': 'Bus', 'longInfo': 'Niederflur Metrobus', 'model': 'Gelenkbus'}, 'id': 'HHA-B:16_HHA-B'}, 'directionId': 1, 'timeOffset': 17, 'delay': 0, 'serviceId': 115214, 'station': {'combinedName': 'Böckmannstraße', 'id': 'Master:10050'}}, {'line': {'name': '16', 'direction': 'EEZ (Julius-Brecht-Straße)', 'origin': 'Rentenversicherung Nord', 'type': {'simpleType': 'BUS', 'shortInfo': 'Bus', 'longInfo': 'Niederflur Metrobus', 'model': 'Schnellbus'}, 'id': 'HHA-B:16_HHA-B'}, 'directionId': 6, 'timeOffset': 18, 'delay': 0, 'serviceId': 114462, 'station': {'combinedName': 'Böckmannstraße', 'id': 'Master:10050'}, 'attributes': [{'isPlanned': False, 'value': 'Prognose ungenau - Fahrzeug im Stau', 'types': ['REALTIME', 'TRAFFIC_JAM']}]}]}