If you print the return of the API’s, ie if you put a print(url.text) within the functions flights and flightPlans, will see that their return are JSON arrays:
[{'AircraftID': 'f11ed126-bce8 etc...
[{'AlternateDestinations': etc...
Notice that they start with [, which indicates that it is a JSON array, that the module json maps to Python lists. That is to say, x and y are lists.
And each element of the list is a dictionary containing the data of a flight. For example, taking the first elements of x (removing some data, to be shorter and clear):
[{'AircraftID': 'f11ed126-bce8-46ef-9265-69191c354575', ... 'VerticalSpeed': 3.269327180532855e-06},
{'AircraftID': '230ec095-5e36-4637-ba2f-68831b31e891', ... 'VerticalSpeed': 30.30824089050293},
{'AircraftID': 'bec63a00-a483-4427-a076-0f76dba0ee97', ... 'VerticalSpeed': 16.32938575744629},
... ]
Each element in the list is a dictionary (delimited by {}), and the elements are separated by commas.
So when you do for i in x, at each iteration the variable i contains one of the list elements, that is, one of the dictionaries. This means that you do not need to do x[i]['DisplayName'], just do i['DisplayName'], for i is already a dictionary.
In addition, for each element of x, you should check if he’s on y, and for that you need to go through the y every time, since there does not seem to be a guarantee of order of the returned data, then nothing guarantees that a FlighID of x is in the same position in y: the first FlighID of x may be at any position on the list y - not necessarily in the first - so you have to go through the whole list y for each element of x:
for flight in x:
for plan in y:
if flight['FlightID'] == plan['FlightID']:
print(plan['Waypoints'])
Accessing now, I found several cases (follow the first ones, just to illustrate how the return would look):
['WPT', 'CELAK', 'BUGNE', 'CUSBU', 'KSEA']
['WPT', 'SYDNM', 'SYDSM', 'YSSY', 'MA34L', 'ATRET', 'DEENA', 'D178N', 'TAMMI', 'NOLEM', 'NFTF', 'NIUE', 'D064W', 'MMJC', 'D248H', 'PLAZA', 'MEBIP', 'MMMX']
['WPT', 'LAMBY', 'MCCAL', 'XS99', 'HUDZY', 'ZAPPO', 'GRIEG', 'KERNS', 'SOROY', 'KIAH']
Not directly related, but the functions that get the data from x and y are practically the same (only changes the URL), so would simplify a little, creating only a function to get the data of any URL.
And also worth give more meaningful names for the variables x and y (may seem like a silly detail, but better names help when programming):
def get_json(url):
return json.loads(requests.get(url).text)
flights = get_json('http://infinite-flight-public-api.cloudapp.net/v1/Flights.aspx?'
'apikey=78879b1d-3ba3-47de-8e50-162f35dc6e04&sessionid=7e5dcd44-1fb5-49cc-bc2c-a9aab1f6a856')
plans = get_json('http://infinite-flight-public-api.cloudapp.net/v1/GetFlightPlans.aspx?'
'apikey=78879b1d-3ba3-47de-8e50-162f35dc6e04&sessionid=7e5dcd44-1fb5-49cc-bc2c-a9aab1f6a856')
for flight in flights:
for plan in plans:
if flight['FlightID'] == plan['FlightID']:
print(plan['Waypoints'])
Good! I didn’t think about the detail of the elements' orders.
– Sidon