1

I'm looking for some advice on how to handle JSON/dictionary data that I'm pulling from an API. The API I am trying to use is a Python implementation of FlightRadar24, the popular flight tracking service. What I am attempting to accomplish is get information for every flight it currently tracks, i.e. the callsign, aircraft type, altitude, destination, and so on.

This is done in a bit of a roundabout way by querying all the flights for a particular airline like so:

import flightradar24
fr24 = flightradar24.Api()
delta_flights = fr24.get_flights("ICE")

This simple call above will return all the information for all flights for Icelandair that are currently active. Now I can loop through this constantly calling every airline to get a dictionary of all flights that are currently in the air. However the structure that it returns the flights in is what's confusing me. Below is an example of the dictionary structure it returns when calling just for ICE.

{'full_count': 15510, 'version': 4, '289610b7': ['4CC2AD', 41.0623, -4.4359, 125, 38000, 499, '1161', 'F-LERL4', 'B752', 'TF-FIV', 1627561427, 'KEF', 'ALC', 'FI1582', 0, 0, 'ICE1582', 0, 'ICE'], '28962687': ['4CC52C', 56.2083, -57.1494, 219, 38000, 430, '0000', 'T-F5M', 'B38M', 'TF-ICN', 1627561428, 'KEF', 'BOS', 'FI635', 0, 0, 'ICE635', 0, 'ICE'], '28963993': ['4CC330', 67.8567, -39.1096, 293, 23975, 281, '2772', 'F-BGKK1', 'DH8B', 'TF-FXG', 1627561420, 'RKV', '', 'FI1021', 0, -64, 'ICE1021', 0, 'ICE'], '2896784d': ['4CC2A6', 60.4406, 8.6048, 294, 34975, 490, '0244', 'F-ENRY3', 'B752', 'TF-FIO', 1627561421, 'OSL', 'KEF', 'FI319', 0, 2560, 'ICE319', 0, 'ICE'], '28967c03': ['4CC52B', 47.4731, 7.002, 290, 23800, 434, '3053', 'F-LSZH2', 'B39M', 'TF-ICA', 1627561427, 'ZRH', 'KEF', 'FI569', 0, 1600, 'ICE9L', 0, 'ICE'], '28967d8f': ['4CC50E', 60.3222, 15.8041, 300, 27550, 448, '0263', 'F-ESOW6', 'B38M', 'TF-ICE', 1627561426, 'ARN', 'KEF', 'FI307', 0, 1536, 'ICE307', 0, 'ICE'], '28967e4c': ['4CC50F', 51.0326, 3.9563, 292, 11625, 294, '4421', 'F-EHRD5', 'B38M', 'TF-ICY', 1627561427, 'BRU', 'KEF', 'FI555', 0, 2688, 'ICE67K', 0, 'ICE'], '28968886': ['4CC27E', 52.3049, 4.774, 140, 0, 5, '2113', 'F-EHAM1', 'B763', 'TF-ISP', 1627561427, 'AMS', 'KEF', 'FI501', 1, 0, 'ICE501', 0, 'ICE'], '2896894c': ['4CC279', 51.4691, -0.4405, 180, 0, 1, '6311', 'F-EGLL2', 'B763', 'TF-ISO', 1627561346, 'LHR', 'KEF', 'FI451', 1, 0, 'ICE451', 0, 'ICE'], '28968b37': ['4CC27F', 50.0507, 8.5915, 253, 0, 0, '2555', 'F-EDDF6', 'B763', 'TF-ISW', 1627561427, 'FRA', 'KEF', 'FI521', 1, 0, 'ICE521', 0, 'ICE'], '28968baf': ['4CC27C', 55.6256, 12.6421, 64, 0, 5, '0430', 'F-EKCH2', 'B752', 'TF-ISR', 1627561425, 'CPH', 'KEF', 'FI207', 1, 0, 'ICE93R', 0, 'ICE'], '28968be0': ['4CC331', 64.1305, -21.9461, 115, 0, 13, '4111', 'F-BIRK3', 'DH8B', 'TF-FXK', 1627561427, 'RKV', 'GOH', 'FI111', 1, 0, 'ICE111', 0, 'ICE'], 'stats': {'total': {'ads-b': 12468, 'mlat': 796, 'faa': 297, 'flarm': 1478, 'estimated': 193, 'satellite': 171, 'other': 0}, 'visible': {'ads-b': 11, 'mlat': 0, 'faa': 1, 'flarm': 0, 'estimated': 0, 'satellite': 0, 'other': 0}}}

What I've discovered so far is that we've got a dictionary here with a unique key for each flight, and then the dictionary values for that flight are within a list. Within that list is the information I want to extract such as aircraft type, altitude, position, destination, etc. So printing simply the keys for this above dictionary gives us:

dict_keys(['full_count', 'version', '289610b7', '28962687', '28963993', '2896784d', '28967c03', '28967d8f', '28967e4c', '28968886', '28968b37', '28968baf', '28968be0', 'stats'])

With the keys, full_count and version are the total number of flights tracked by FR24, the FR24 version, and then stats is the breakdown of how all flights that we have querying are tracked, so of the 11 keys (flights), all 11 are tracked using ADS-B. The key values themselves are pretty meaningless and just a unique identifier, what I really want is the values that the key holds.

What I am looking to do is extract information from the values of this dictionary. So I'm looking for a nice list of each flight with callsign, altitude, destination etc. However I am not sure how to loop through this dictionary to extract all the values correctly.

I hope this was a clear explanation and thanks for any help.

1
  • What do each of the values in the list mean? In order? Commented Jul 29, 2021 at 12:41

2 Answers 2

1

The values in your dictionaries are list. You may loop through them as you would do with any other list.

flights = {'full_count': 15510, 'version': 4, '289610b7': ['4CC2AD', 41.0623, -4.4359, 125, 38000, 499, '1161', 'F-LERL4', 'B752', 'TF-FIV', 1627561427, 'KEF', 'ALC', 'FI1582', 0, 0, 'ICE1582', 0, 'ICE'], '28962687': ['4CC52C', 56.2083, -57.1494, 219, 38000, 430, '0000', 'T-F5M', 'B38M', 'TF-ICN', 1627561428, 'KEF', 'BOS', 'FI635', 0, 0, 'ICE635', 0, 'ICE'], '28963993': ['4CC330', 67.8567, -39.1096, 293, 23975, 281, '2772', 'F-BGKK1', 'DH8B', 'TF-FXG', 1627561420, 'RKV', '', 'FI1021', 0, -64, 'ICE1021', 0, 'ICE'], '2896784d': ['4CC2A6', 60.4406, 8.6048, 294, 34975, 490, '0244', 'F-ENRY3', 'B752', 'TF-FIO', 1627561421, 'OSL', 'KEF', 'FI319', 0, 2560, 'ICE319', 0, 'ICE'], '28967c03': ['4CC52B', 47.4731, 7.002, 290, 23800, 434, '3053', 'F-LSZH2', 'B39M', 'TF-ICA', 1627561427, 'ZRH', 'KEF', 'FI569', 0, 1600, 'ICE9L', 0, 'ICE'], '28967d8f': ['4CC50E', 60.3222, 15.8041, 300, 27550, 448, '0263', 'F-ESOW6', 'B38M', 'TF-ICE', 1627561426, 'ARN', 'KEF', 'FI307', 0, 1536, 'ICE307', 0, 'ICE'], '28967e4c': ['4CC50F', 51.0326, 3.9563, 292, 11625, 294, '4421', 'F-EHRD5', 'B38M', 'TF-ICY', 1627561427, 'BRU', 'KEF', 'FI555', 0, 2688, 'ICE67K', 0, 'ICE'], '28968886': ['4CC27E', 52.3049, 4.774, 140, 0, 5, '2113', 'F-EHAM1', 'B763', 'TF-ISP', 1627561427, 'AMS', 'KEF', 'FI501', 1, 0, 'ICE501', 0, 'ICE'], '2896894c': ['4CC279', 51.4691, -0.4405, 180, 0, 1, '6311', 'F-EGLL2', 'B763', 'TF-ISO', 1627561346, 'LHR', 'KEF', 'FI451', 1, 0, 'ICE451', 0, 'ICE'], '28968b37': ['4CC27F', 50.0507, 8.5915, 253, 0, 0, '2555', 'F-EDDF6', 'B763', 'TF-ISW', 1627561427, 'FRA', 'KEF', 'FI521', 1, 0, 'ICE521', 0, 'ICE'], '28968baf': ['4CC27C', 55.6256, 12.6421, 64, 0, 5, '0430', 'F-EKCH2', 'B752', 'TF-ISR', 1627561425, 'CPH', 'KEF', 'FI207', 1, 0, 'ICE93R', 0, 'ICE'], '28968be0': ['4CC331', 64.1305, -21.9461, 115, 0, 13, '4111', 'F-BIRK3', 'DH8B', 'TF-FXK', 1627561427, 'RKV', 'GOH', 'FI111', 1, 0, 'ICE111', 0, 'ICE'], 'stats': {'total': {'ads-b': 12468, 'mlat': 796, 'faa': 297, 'flarm': 1478, 'estimated': 193, 'satellite': 171, 'other': 0}, 'visible': {'ads-b': 11, 'mlat': 0, 'faa': 1, 'flarm': 0, 'estimated': 0, 'satellite': 0, 'other': 0}}}
for key in flights:
    if isinstance(flights[key], list):
        for val in flights[key]:
            print(val) ## do anything

Using isinstance to confirm that the value is of list datatype so we can proceed with iteration. (Some values in your dictionary are of integer datatype this will take care of that).

Sign up to request clarification or add additional context in comments.

Comments

0

You can go through your keys and try to convert them as int. If it's succesful, take the first element of the array:

flight_dict = {'full_count': 15510, 'version': 4, '289610b7': ['4CC2AD', 41.0623, -4.4359, 125, 38000, 499, '1161', 'F-LERL4', 'B752', 'TF-FIV', 1627561427, 'KEF', 'ALC', 'FI1582', 0, 0, 'ICE1582', 0, 'ICE'], '28962687': ['4CC52C', 56.2083, -57.1494, 219, 38000, 430, '0000', 'T-F5M', 'B38M', 'TF-ICN', 1627561428, 'KEF', 'BOS', 'FI635', 0, 0, 'ICE635', 0, 'ICE'], '28963993': ['4CC330', 67.8567, -39.1096, 293, 23975, 281, '2772', 'F-BGKK1', 'DH8B', 'TF-FXG', 1627561420, 'RKV', '', 'FI1021', 0, -64, 'ICE1021', 0, 'ICE'], '2896784d': ['4CC2A6', 60.4406, 8.6048, 294, 34975, 490, '0244', 'F-ENRY3', 'B752', 'TF-FIO', 1627561421, 'OSL', 'KEF', 'FI319', 0, 2560, 'ICE319', 0, 'ICE'], '28967c03': ['4CC52B', 47.4731, 7.002, 290, 23800, 434, '3053', 'F-LSZH2', 'B39M', 'TF-ICA', 1627561427, 'ZRH', 'KEF', 'FI569', 0, 1600, 'ICE9L', 0, 'ICE'], '28967d8f': ['4CC50E', 60.3222, 15.8041, 300, 27550, 448, '0263', 'F-ESOW6', 'B38M', 'TF-ICE', 1627561426, 'ARN', 'KEF', 'FI307', 0, 1536, 'ICE307', 0, 'ICE'], '28967e4c': ['4CC50F', 51.0326, 3.9563, 292, 11625, 294, '4421', 'F-EHRD5', 'B38M', 'TF-ICY', 1627561427, 'BRU', 'KEF', 'FI555', 0, 2688, 'ICE67K', 0, 'ICE'], '28968886': ['4CC27E', 52.3049, 4.774, 140, 0, 5, '2113', 'F-EHAM1', 'B763', 'TF-ISP', 1627561427, 'AMS', 'KEF', 'FI501', 1, 0, 'ICE501', 0, 'ICE'], '2896894c': ['4CC279', 51.4691, -0.4405, 180, 0, 1, '6311', 'F-EGLL2', 'B763', 'TF-ISO', 1627561346, 'LHR', 'KEF', 'FI451', 1, 0, 'ICE451', 0, 'ICE'], '28968b37': ['4CC27F', 50.0507, 8.5915, 253, 0, 0, '2555', 'F-EDDF6', 'B763', 'TF-ISW', 1627561427, 'FRA', 'KEF', 'FI521', 1, 0, 'ICE521', 0, 'ICE'], '28968baf': ['4CC27C', 55.6256, 12.6421, 64, 0, 5, '0430', 'F-EKCH2', 'B752', 'TF-ISR', 1627561425, 'CPH', 'KEF', 'FI207', 1, 0, 'ICE93R', 0, 'ICE'], '28968be0': ['4CC331', 64.1305, -21.9461, 115, 0, 13, '4111', 'F-BIRK3', 'DH8B', 'TF-FXK', 1627561427, 'RKV', 'GOH', 'FI111', 1, 0, 'ICE111', 0, 'ICE'], 'stats': {'total': {'ads-b': 12468, 'mlat': 796, 'faa': 297, 'flarm': 1478, 'estimated': 193, 'satellite': 171, 'other': 0}, 'visible': {'ads-b': 11, 'mlat': 0, 'faa': 1, 'flarm': 0, 'estimated': 0, 'satellite': 0, 'other': 0}}}

for key in flight_dict:
    try:
        key_hex = bytes.fromhex(key)
        print(flight_dict[key])
    except Exception:
        pass

EDIT: I might have slightly misunderstood the question, other values can be accessed with appropriate index or by looping through flight_dict[key]

EDIT2: I also oversaw that flight ref are in hex and not int (:facepalm:). I removed the index so that it would print the whole array :-)

3 Comments

Thanks very much for your reply - the flight ref key being in hex was what confused me. Your solution looks great - as an example of what it will output now, I get a dict of lists like below: ['7C146E', -37.6677, 144.8537, 303, 0, 1, '4243', 'F-YMMB1', 'A332', 'VH-EBK', 1627614062, 'MEL', 'PER', 'QF775', 1, 0, 'QFA775', 0, 'QFA'] So that's one flight, except each flight has its own list. However how would I then extract information from these lists? Say I want to pull the altitude from every single flight (list), how would I do that? Unsure how I would loop through it all.
See also Tasbiha's answer. You can print values separately in a loop with for val in flight_dict[key]:. If you want access to a particular property (I guess altitude is the 4th element in feet i.e. at index 3) : flight_dict[key][3].
You can also print elements separated by a comma to export them as CSV and later import in Excel for example: print(','.join(flight_dict[key]))

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.