string indices must be integers

Asked

Viewed 690 times

-1

I have a Json that returns this data:

"errorCode": 0,
    "result": [
        {
            "username": "Captain Samafa",
            "callsign": "24K",
            "latitude": 39.7100376616406,
            "longitude": 59.01369415537506,
            "altitude": 33900.229864846835,
            "speed": 476.06915753461686,
            "verticalSpeed": 4.759671688079834,
            "track": 197.1294708251953,
            "lastReport": "2020-10-06 16:42:52Z",
            "flightId": "1854b0ee-ba71-473c-86b2-ac1935792dee",
            "userId": "27786ee9-8bcf-4a5b-9055-ebc0a5edaf39",
            "aircraftId": "230ec095-5e36-4637-ba2f-68831b31e891",
            "liveryId": "d09a1283-b2ff-4cd2-92f4-1d54c2a46c1b",
            "heading": 201.18605,
            "virtualOrganization": null
        },
        {
            "username": "IFATCLee3440",
            "callsign": "Madras 12",
            "latitude": -52.60996492913441,
            "longitude": -54.92538650405199,
            "altitude": 29345.708025272826,
            "speed": 422.9940309420305,
            "verticalSpeed": -1455.298828125,
            "track": 289.4452819824219,
            "lastReport": "2020-10-06 16:42:52Z",
            "flightId": "6d7feaf2-5705-4b3e-8003-0f984259e769",
            "userId": "0c326e97-ee4c-4246-9588-11b87f1bed9f",
            "aircraftId": "e59fa7b4-b708-4480-aebd-26659a4f312b",
            "liveryId": "9508c65e-2992-4759-b382-3c0e75c24923",
            "heading": 290.7723,
            "virtualOrganization": null
        },
        {
            "username": "ajfl",
            "callsign": "Southern Air 120",
            "latitude": 49.95203285892042,
            "longitude": 7.273932297661211,
            "altitude": 1663.5415957890098,
            "speed": 0.00031879721291165077,
            "verticalSpeed": 0.032275911420583725,
            "track": 88.79496765136719,
            "lastReport": "2020-10-06 16:42:52Z",
            "flightId": "60e0cec7-9e98-4a3b-8d88-cb2fbd073cb6",
            "userId": "f9f8adbd-682f-47f3-9e52-4ac0a1c1d195",
            "aircraftId": "6925c030-a868-49cc-adc8-7025537c51ca",
            "liveryId": "0ffc1641-3ab9-4b16-a701-033ab41ed08d",
            "heading": 122.89578,
            "virtualOrganization": null
        },
        {
            "username": "arjun",
            "callsign": "Airbus 010 Heavy",
            "latitude": -1.4518363199159487,
            "longitude": 29.408336730183667,
            "altitude": 11000.026394873705,
            "speed": 358.0320414644207,
            "verticalSpeed": 23.87726402282715,
            "track": 89.05824279785156,
            "lastReport": "2020-10-06 16:42:52Z",
            "flightId": "f152d38f-63cb-4077-8337-f58ebd1fe618",
            "userId": "496c096c-4e59-44b2-9c94-acb74c668ff5",
            "aircraftId": "230ec095-5e36-4637-ba2f-68831b31e891",
            "liveryId": "f334063e-5103-4b3b-bd15-b1b2fdb220fd",
            "heading": 90.01053,
            "virtualOrganization": null
        },
        {
            "username": "GER-ALLIANCE MuKa7",
            "callsign": "MK-7",
            "latitude": 64.4744059592217,
            "longitude": 13.611431742529339,
            "altitude": 34999.82801008071,
            "speed": 489.1741898339633,
            "verticalSpeed": -3.9575376510620117,
            "track": 195.6558837890625,
            "lastReport": "2020-10-06 16:42:52Z",
            "flightId": "35847c52-12c2-4af2-adbe-ec4d3483f1d8",
            "userId": "69884110-69dd-498f-a25a-ec88a384a9c8",
            "aircraftId": "bec63a00-a483-4427-a076-0f76dba0ee97",
            "liveryId": "a20ebf14-830a-4f05-b173-2dcd30c5a586",
            "heading": 194.24469,
            "virtualOrganization": null
        }

I’m trying to filter by Indice "virtualOrganization"

flight = get_json('https://api.infiniteflight.com/public/v2/flights/7e5dcd44-1fb5-49cc-bc2c-a9aab1f6a856?apikey=[key]')
        
       
for x in flight:    
  if "IFATC" in x["virtualOrganization"]:
    print(x['username'])
                

Only it’s returned me error that I’m not able to solve:

Typeerror: string indices must be integers

1 answer

0


I imagine that flight is this JSON presented, so it seems to me that you are doing the for at the wrong level.

The x will be the "errorCode" and "result", to confirm you just leave your for thus:

for x in flight:    
    print( x )
    #if "IFATC" in x["virtualOrganization"]:
    #    print(x['username'])

See working on ideone

For this reason this error is occurring:

TypeError: string indices must be integers

x is a string and you are looking for a key, the error informs that in strings the indexes have to be integers. Ex:

x = "errorCode"

# o indice 5
print( x[5] ) # C

# a partir do indice 5
print( x[5:] ) # Code

# até o indice 5
print( x[:5]) # error

# toda a string ao contrario
print( x[::-1]) # edoCrorre

See working on ideone

It seems to me you want to walk the flight["result"], as the code below:

for x in flight["result"]:
    print( x )
    #if "IFATC" in x["virtualOrganization"]:
    #    print(x['username'])

See working on ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.