0

I have a google API response, but I can't solve how to reach the "formatted_address" in the JSON response in python. I tried:

import json
with open('test.json', mode='r', encoding='utf-8') as f:
  data = json.load(f)

json_rep = data["results"]
print(json_rep)
aa = json.load(json_rep)
aaa = aa["formatted_address"]
print(aaa)

I need to get the formatted_address value which is - "formatted_address" : "M. Sleževičiaus g. 7, Vilnius 06326, Lithuania",

JSON RESPONSE:

{
   "plus_code" : {
      "compound_code" : "P7Q2+3M Vilnius, Lithuania",
      "global_code" : "9G67P7Q2+3M"
   },
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "7",
               "short_name" : "7",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "M. Sleževičiaus gatvė",
               "short_name" : "M. Sleževičiaus g.",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Vilnius",
               "short_name" : "Vilnius",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vilniaus miesto savivaldybė",
               "short_name" : "Vilniaus m. sav.",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilniaus apskritis",
               "short_name" : "Vilniaus apskr.",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "06326",
               "short_name" : "06326",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "M. Sleževičiaus g. 7, Vilnius 06326, Lithuania",
         "geometry" : {
            "location" : {
               "lat" : 54.73771790000001,
               "lng" : 25.2518586
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.73906688029151,
                  "lng" : 25.2532075802915
               },
               "southwest" : {
                  "lat" : 54.73636891970851,
                  "lng" : 25.2505096197085
               }
            }
         },
         "place_id" : "ChIJC8ibiKGR3UYRZyClqH26vYE",
         "plus_code" : {
            "compound_code" : "P7Q2+3P Vilnius, Lithuania",
            "global_code" : "9G67P7Q2+3P"
         },
         "types" : [ "establishment", "point_of_interest" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "7",
               "short_name" : "7",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "M. Sleževičiaus gatvė",
               "short_name" : "M. Sleževičiaus g.",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Vilnius",
               "short_name" : "Vilnius",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vilniaus miesto savivaldybė",
               "short_name" : "Vilniaus m. sav.",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilniaus apskritis",
               "short_name" : "Vilniaus apskr.",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "06326",
               "short_name" : "06326",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "M. Sleževičiaus g. 7, Vilnius 06326, Lithuania",
         "geometry" : {
            "location" : {
               "lat" : 54.737146,
               "lng" : 25.252066
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.7384949802915,
                  "lng" : 25.2534149802915
               },
               "southwest" : {
                  "lat" : 54.7357970197085,
                  "lng" : 25.2507170197085
               }
            }
         },
         "place_id" : "ChIJIxUZgxGR3UYR6ez4mTMiSLQ",
         "plus_code" : {
            "compound_code" : "P7P2+VR Vilnius, Lithuania",
            "global_code" : "9G67P7P2+VR"
         },
         "types" : [ "street_address" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Unnamed Road",
               "short_name" : "Unnamed Road",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Vilnius",
               "short_name" : "Vilnius",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vilniaus miesto savivaldybė",
               "short_name" : "Vilniaus m. sav.",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilniaus apskritis",
               "short_name" : "Vilniaus apskr.",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "06326",
               "short_name" : "06326",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Unnamed Road, Vilnius 06326, Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 54.7377477,
                  "lng" : 25.2520756
               },
               "southwest" : {
                  "lat" : 54.73754590000001,
                  "lng" : 25.2512685
               }
            },
            "location" : {
               "lat" : 54.7376486,
               "lng" : 25.2516736
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.7389957802915,
                  "lng" : 25.2530210302915
               },
               "southwest" : {
                  "lat" : 54.7362978197085,
                  "lng" : 25.2503230697085
               }
            }
         },
         "place_id" : "ChIJ4WCnkBGR3UYRWLeQUpD2P9w",
         "types" : [ "route" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "06326",
               "short_name" : "06326",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Vilnius",
               "short_name" : "Vilnius",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vilnius city municipality",
               "short_name" : "Vilnius city municipality",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilnius County",
               "short_name" : "Vilnius County",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Vilnius 06326, Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 54.739305,
                  "lng" : 25.2588261
               },
               "southwest" : {
                  "lat" : 54.733427,
                  "lng" : 25.2476399
               }
            },
            "location" : {
               "lat" : 54.7364821,
               "lng" : 25.2555399
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.739305,
                  "lng" : 25.2588261
               },
               "southwest" : {
                  "lat" : 54.733427,
                  "lng" : 25.2476399
               }
            }
         },
         "place_id" : "ChIJWUBH5BOR3UYR4paeLozRABM",
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Fabijoniškės",
               "short_name" : "Fabijoniškės",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Vilnius",
               "short_name" : "Vilnius",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vilnius city municipality",
               "short_name" : "Vilnius city municipality",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilnius County",
               "short_name" : "Vilnius County",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Fabijoniškės, Vilnius, Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 54.7576305,
                  "lng" : 25.2703037
               },
               "southwest" : {
                  "lat" : 54.7194219,
                  "lng" : 25.214688
               }
            },
            "location" : {
               "lat" : 54.73416659999999,
               "lng" : 25.2494496
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.7576305,
                  "lng" : 25.2703037
               },
               "southwest" : {
                  "lat" : 54.7194219,
                  "lng" : 25.214688
               }
            }
         },
         "place_id" : "ChIJqzx2KAyR3UYRyei9NObcm4Q",
         "types" : [ "administrative_area_level_3", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Fabijoniškės",
               "short_name" : "Fabijoniškės",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Vilnius",
               "short_name" : "Vilnius",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vilnius city municipality",
               "short_name" : "Vilnius city municipality",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilnius County",
               "short_name" : "Vilnius County",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Fabijoniškės, Vilnius, Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 54.7588229,
                  "lng" : 25.270351
               },
               "southwest" : {
                  "lat" : 54.72018689999999,
                  "lng" : 25.214302
               }
            },
            "location" : {
               "lat" : 54.7352291,
               "lng" : 25.2467146
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.7588229,
                  "lng" : 25.270351
               },
               "southwest" : {
                  "lat" : 54.72018689999999,
                  "lng" : 25.214302
               }
            }
         },
         "place_id" : "ChIJbWDoSAyR3UYR4UZFTI3RACY",
         "types" : [ "neighborhood", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Vilnius",
               "short_name" : "Vilnius",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Vilnius city municipality",
               "short_name" : "Vilnius city municipality",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilnius County",
               "short_name" : "Vilnius County",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Vilnius, Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 54.832544,
                  "lng" : 25.4814883
               },
               "southwest" : {
                  "lat" : 54.567798,
                  "lng" : 25.024376
               }
            },
            "location" : {
               "lat" : 54.6871555,
               "lng" : 25.2796514
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.832544,
                  "lng" : 25.4814883
               },
               "southwest" : {
                  "lat" : 54.567798,
                  "lng" : 25.024376
               }
            }
         },
         "place_id" : "ChIJ9QhkXPuT3UYRQNzpcIzRAAQ",
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Vilnius city municipality",
               "short_name" : "Vilnius city municipality",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Vilnius County",
               "short_name" : "Vilnius County",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Vilnius city municipality, Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 54.832544,
                  "lng" : 25.4814883
               },
               "southwest" : {
                  "lat" : 54.567798,
                  "lng" : 25.024376
               }
            },
            "location" : {
               "lat" : 54.6710761,
               "lng" : 25.2878721
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 54.832544,
                  "lng" : 25.4814883
               },
               "southwest" : {
                  "lat" : 54.567798,
                  "lng" : 25.024376
               }
            }
         },
         "place_id" : "ChIJ7epZQvuT3UYRg4dk0tyVXHU",
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Vilnius County",
               "short_name" : "Vilnius County",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Vilnius County, Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 55.5174369,
                  "lng" : 26.760213
               },
               "southwest" : {
                  "lat" : 54.1276583,
                  "lng" : 24.386375
               }
            },
            "location" : {
               "lat" : 54.8086502,
               "lng" : 25.2182139
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 55.5174369,
                  "lng" : 26.760213
               },
               "southwest" : {
                  "lat" : 54.1276583,
                  "lng" : 24.386375
               }
            }
         },
         "place_id" : "ChIJH4KkkAKi3UYRsNjpcIzRAAM",
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Lithuania",
               "short_name" : "LT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Lithuania",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 56.45032089999999,
                  "lng" : 26.835523
               },
               "southwest" : {
                  "lat" : 53.8967949,
                  "lng" : 20.931
               }
            },
            "location" : {
               "lat" : 55.169438,
               "lng" : 23.881275
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 56.45032089999999,
                  "lng" : 26.835523
               },
               "southwest" : {
                  "lat" : 53.8967949,
                  "lng" : 20.931
               }
            }
         },
         "place_id" : "ChIJE74zDxSU3UYRubpdpdNUCvM",
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}
6
  • No need to json.load, simply use data["results"]["formatted_address"] Commented Jan 7, 2020 at 16:33
  • list indices must be integers or slices, not str Commented Jan 7, 2020 at 16:39
  • Do you have to get all the formatted_address? Commented Jan 7, 2020 at 16:39
  • 1
    You have a list of objects, each one has formatted_address field. The first two objects of the results list have the address that you want. If you need only the first you can do data["results"][0]["formatted_address"] which is M. Sle..., otherwise you can put in a for loop and get all the addresses Commented Jan 7, 2020 at 16:45
  • 1
    Thaks man it worked Commented Jan 7, 2020 at 16:47

1 Answer 1

1

You should not need to call json.load more than once. The first call will parse the entire json structure into a nested dictionary. So your example should become:

import json
with open('test.json', mode='r', encoding='utf-8') as f:
      data = json.load(f)

json_rep = data["results"]
print(json_rep)
aa = json_rep["formatted_address"]
print(aa)
Sign up to request clarification or add additional context in comments.

1 Comment

list indices must be integers or slices, not str

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.