How to extract data from complex Json using python
<pre><code>{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4224484,
               "lng" : -122.0843249
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4237973802915,
                  "lng" : -122.0829759197085
               },
               "southwest" : {
                  "lat" : 37.4210994197085,
                  "lng" : -122.0856738802915
               }
            }
         },
         "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}
</code></pre>

<p>How can i retrieve <code>formatted_address</code>,<code>Lat</code> and <code>Long</code> under <code>geometry&gt;Location</code> etc. from json. Most of the json tutorials are explained with simple json file.
Kindly help me on this and help  me find some good tutorials</p>

<p>Actually, simple JSONs and complex JSONs are not that different. You can say that a <em>complex JSON</em> has many <em>simpler JSONs</em> inside, so if you know how to get data from simple ones, you end up knowing how to get data from complexes </p>

<p>Anyway, think of JSON objects as <code>dictionaries</code> and JSON arrays as <code>lists</code>. To get data from dicts, use the <code>key</code> for a value. To get data from lists, use the <code>index</code> (position) of a value.</p>

<p>So, in your case:</p>

<pre><code>lat = json["results"][0]["geometry"]["location"]["lat"]
long = json["results"][0]["geometry"]["location"]["lng"]
</code></pre>

<p>and</p>

<pre><code>formatted_address = json["results"][0]["formatted_address"]
</code></pre>

<p>Notice how the navigation through keys occur, and how you get deeper as you retrieve values from its keys</p>

python json