Search code examples
pythonjsonpython-3.xapiamadeus

Retrieve Daily Rate - Amadeus Car Rental API


I am having trouble retrieving the daily rate in the JSON file returned from our Amadeus Car Rental API.

I am getting a code error for the daily_price variable (list indices must be integers or slices, not str).

w, h = 3, i
final_list = [[0 for x in range(w)] for y in range(h)]
while z < i:
    company_name = (json_data["results"][z]["provider"]["company_name"])
    daily_price = (json_data["results"][z]["cars"]["rates"]["price"]["amount"])
    #total_price = (json_data["results"][z]["cars"]["estimated_total"]["amount"])
    final_list[z][0] = company_name
    final_list[z][1] = daily_price
    #final_list[z][1] = total_price
    final_list[z][2] = z
    z = z + 1
return HttpResponse(final_list)

I have seen this before when I have not navigated the JSON structure correctly. I have tried a mixture of formats, but not sure to retrieve the daily rate nested in the JSON.

"results" : [ {
    "provider" : {
      "company_code" : "ZU",
      "company_name" : "AUTO EUROPE"
    },
    "branch_id" : "SLCT01",
    "location" : {
      "latitude" : 40.78994,
      "longitude" : -111.98125
    },
    "airport" : "SLC",
    "address" : {
      "line1" : "SALT LAKE CITY INTERNATIONAL AIRPORT",
      "city" : "SALT LAKE CITY",
      "country" : "US"
    },
    "cars" : [ {
      "vehicle_info" : {
        "acriss_code" : "ED",
        "transmission" : "No information available",
        "fuel" : "No information available",
        "category" : "Economy",
        "type" : "4-5 Door"
      },
      "rates" : [ {
        "type" : "DAILY",
        "price" : {
          "amount" : "26.00",
          "currency" : "USD"
        }
      } ],

Solution

  • you missed that some of them is a dict inside a list

    daily_price = json_data["results"][z]["cars"][0]["rates"][0]["price"]["amount"]