Search code examples
javascriptjqueryjsonvariablesstore

How to get a specific information from Json output


I am doing a school project and I am working with json output form Google Geocoding API.

I don't know how to store the "formatted_address" (see below) of the output in a variable so I can use this information later.

Here is a piece of the json output that I am working with.

"results" : [
  {
     "address_components" : [
        {
           "long_name" : "11A",
           "short_name" : "11A",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Albyn Place",
           "short_name" : "Albyn Pl",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Aberdeen",
           "short_name" : "Aberdeen",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Aberdeen",
           "short_name" : "Aberdeen",
           "types" : [ "postal_town" ]
        },
        {
           "long_name" : "Aberdeen City",
           "short_name" : "Aberdeen City",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "United Kingdom",
           "short_name" : "GB",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "AB10 1YE",
           "short_name" : "AB10 1YE",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "11A Albyn Place, Aberdeen, Aberdeen City AB10 1YE, UK",
     "geometry" : {
        "location" : {
           "lat" : 57.1425663,
           "lng" : -2.1144957
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 57.1439152802915,
              "lng" : -2.113146719708498
           },
           "southwest" : {
              "lat" : 57.1412173197085,
              "lng" : -2.115844680291502
           }
        }
     },
     "place_id" : "ChIJ89KpnycOhEgR96EdolCIM8U",
     "types" : [ "street_address" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Union Grove (o/s 82)",
           "short_name" : "Union Grove (o/s 82)",
           "types" : [ "bus_station", "transit_station", "establishment" ]
        },
        {
           "long_name" : "Aberdeen",
           "short_name" : "Aberdeen",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Aberdeen",
           "short_name" : "Aberdeen",
           "types" : [ "postal_town" ]
        },
        {
           "long_name" : "Aberdeen City",
           "short_name" : "Aberdeen City",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "United Kingdom",
           "short_name" : "GB",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "AB10",
           "short_name" : "AB10",
           "types" : [ "postal_code_prefix", "postal_code" ]
        }
     ],
     "formatted_address" : "Union Grove (o/s 82), Aberdeen, Aberdeen, Aberdeen City AB10, UK",
     "geometry" : {
        "location" : {
           "lat" : 57.141483,
           "lng" : -2.115739
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 57.1428319802915,
              "lng" : -2.114390019708498
           },
           "southwest" : {
              "lat" : 57.1401340197085,
              "lng" : -2.117087980291502
           }
        }
     },
     "place_id" : "ChIJra8KiycOhEgRSR-z6DmOvO8",
     "types" : [ "bus_station", "transit_station", "establishment" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "AB10 1YE",
           "short_name" : "AB10 1YE",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "Aberdeen",
           "short_name" : "Aberdeen",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Aberdeen",
           "short_name" : "Aberdeen",
           "types" : [ "postal_town" ]
        },
        {
           "long_name" : "Aberdeen City",
           "short_name" : "Aberdeen City",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "United Kingdom",
           "short_name" : "GB",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Aberdeen, Aberdeen, Aberdeen City AB10 1YE, UK",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 57.1429049,
              "lng" : -2.1140542
           },
           "southwest" : {
              "lat" : 57.141979,
              "lng" : -2.1148766
           }
        },
        "location" : {
           "lat" : 57.1425663,
           "lng" : -2.1144957
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 57.14379093029149,
              "lng" : -2.113116419708498
           },
           "southwest" : {
              "lat" : 57.1410929697085,
              "lng" : -2.115814380291502
           }
        }
     },
     "place_id" : "ChIJWey5nycOhEgRXR9pzwov_pY",
     "types" : [ "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "AB10",
           "short_name" : "AB10",
           "types" : [ "postal_code_prefix", "postal_code" ]
        },
        {
           "long_name" : "Aberdeen",
           "short_name" : "Aberdeen",
           "types" : [ "postal_town" ]
        },
        {
           "long_name" : "United Kingdom",
           "short_name" : "GB",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Aberdeen AB10, UK",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 57.1511959,
              "lng" : -2.0903783
           },
           "southwest" : {
              "lat" : 57.1094073,
              "lng" : -2.1530504
           }
        },
        "location" : {
           "lat" : 57.131255,
           "lng" : -2.1264712
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 57.1511959,
              "lng" : -2.0903783
           },
           "southwest" : {
              "lat" : 57.1094073,
              "lng" : -2.1530504
           }
        }
     },
     "place_id" : "ChIJOT6RmCAOhEgRRCBB7_j2M30",
     "types" : [ "postal_code_prefix", "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Aberdeen",
           "short_name" : "Aberdeen",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Aberdeen City",
           "short_name" : "Aberdeen City",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Scotland",
           "short_name" : "Scotland",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United Kingdom",
           "short_name" : "GB",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Aberdeen, Aberdeen City, UK",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 57.19565069999999,
              "lng" : -2.0461811
           },
           "southwest" : {
              "lat" : 57.1041518,
              "lng" : -2.2058926
           }
        },
        "location" : {
           "lat" : 57.149717,
           "lng" : -2.094278
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 57.19565069999999,
              "lng" : -2.0461811
           },
           "southwest" : {
              "lat" : 57.1041518,
              "lng" : -2.2058926
           }
        }
     },
     "place_id" : "ChIJSXXXH0wFhEgRcsT0XNoFu-g",
     "types" : [ "locality", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Aberdeen City",
           "short_name" : "Aberdeen City",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Scotland",
           "short_name" : "Scotland",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United Kingdom",
           "short_name" : "GB",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Aberdeen City, UK",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 57.2353521,
              "lng" : -2.0461828
           },
           "southwest" : {
              "lat" : 57.07618890000001,
              "lng" : -2.3609398
           }
        },
        "location" : {
           "lat" : 57.1506564,
           "lng" : -2.1467599
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 57.2353521,
              "lng" : -2.0461828
           },
           "southwest" : {
              "lat" : 57.07618890000001,
              "lng" : -2.3609398
           }
        }
     },
     "place_id" : "ChIJs6qVe2kRhEgRkLBCBhpoDAM",
     "types" : [ "administrative_area_level_2", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Aberdeen",
           "short_name" : "Aberdeen",
           "types" : [ "postal_town" ]
        },
        {
           "long_name" : "United Kingdom",
           "short_name" : "GB",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Aberdeen, UK",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 57.3274181,
              "lng" : -2.0076331
           },
           "southwest" : {
              "lat" : 57.0411197,
              "lng" : -2.3365801
           }
        },
        "location" : {
           "lat" : 57.2177758,
           "lng" : -2.1823124
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 57.3274181,
              "lng" : -2.0076331
           },
           "southwest" : {
              "lat" : 57.0411197,
              "lng" : -2.3365801
           }
        }
     },
     "place_id" : "ChIJe57LATIShEgRdUZWMa_i0ag",
     "types" : [ "postal_town" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Scotland",
           "short_name" : "Scotland",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United Kingdom",
           "short_name" : "GB",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Scotland, UK",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 60.8607515,
              "lng" : -0.7246751
           },
           "southwest" : {
              "lat" : 54.6332381,
              "lng" : -8.6498565
           }
        },
        "location" : {
           "lat" : 56.49067119999999,
           "lng" : -4.2026458
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 60.84567610000001,
              "lng" : -0.7247473999999999
           },
           "southwest" : {
              "lat" : 54.6332381,
              "lng" : -8.6492466
           }
        }
     },
     "place_id" : "ChIJn6HyA8TiYUgRFAfDCdj6wec",
     "types" : [ "administrative_area_level_1", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "United Kingdom",
           "short_name" : "GB",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "United Kingdom",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 60.8606697,
              "lng" : 33.916555
           },
           "southwest" : {
              "lat" : 34.5626034,
              "lng" : -8.649357199999999
           }
        },
        "location" : {
           "lat" : 55.378051,
           "lng" : -3.435973
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 60.856553,
              "lng" : 1.7627096
           },
           "southwest" : {
              "lat" : 49.8669688,
              "lng" : -8.649357199999999
           }
        }
     },
     "place_id" : "ChIJqZHHQhE7WgIReiWIMkOg-MQ",
     "types" : [ "country", "political" ]
  }
],
"status" : "OK"

So far I am trying this in my js file:

var restourantAddressRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+restLatitude+","+restLongtitude+"&key=API_KEY;
//alert(restourantAddressRequest.results.formatted_address);
$("#restaurant_address").html("Address: " + restourantAddressRequest.results.formatted_address);

Solution

  • You want to use dot notation to get to the value you want. The Google V3 Geocoder returns an array so what you're looking for is the following:

    results[0].formatted_address
    

    Here is a fiddle which alerts the formatted address for you:

    http://jsfiddle.net/iamjpg/h39041w2/

    I hope this is helpful.