title: How to work with nested data structures in Elixir
body: <p>I'm trying to work with the Google Maps geocoding API in Elixir though I'm a bit new to the language so working with nested data structures is eluding me.</p>
  
  <p>I'm using HTTPotion to fetch the JSON from the geocoding endpoint and parsing it into Elixir data structures (a series of nested lists and tuples) using JSEX.</p>
  
  <pre><code>defmodule Geocode do
    def fetch do
      HTTPotion.start
      result = HTTPotion.get "http://maps.googleapis.com/maps/api/geocode/json?address=Fairfax,%20Vermont&amp;sensor=false"
      json   = JSEX.decode result.body
    end
  end
  </code></pre>
  
  <p>The following is now assigned to json.</p>
  
  <pre><code>{:ok, [{"results", [[{"address_components", [[{"long_name", "Fairfax"}, {"short_name", "Fairfax"}, {"types", ["locality", "political"]}], [{"long_name", "Franklin"}, {"short_name", "Franklin"}, {"types", ["administrative_area_level_2", "political"]}], [{"long_name", "Vermont"}, {"short_name", "VT"}, {"types", ["administrative_area_level_1", "political"]}], [{"long_name", "United States"}, {"short_name", "US"}, {"types", ["country", "political"]}]]}, {"formatted_address", "Fairfax, VT, USA"}, {"geometry", [{"bounds", [{"northeast", [{"lat", 44.772892}, {"lng", -72.92268890000001}]}, {"southwest", [{"lat", 44.6330508}, {"lng", -73.08477409999999}]}]}, {"location", [{"lat", 44.6654963}, {"lng", -73.01032}]}, {"location_type", "APPROXIMATE"}, {"viewport", [{"northeast", [{"lat", 44.772892}, {"lng", -72.92268890000001}]}, {"southwest", [{"lat", 44.6330508}, {"lng", -73.08477409999999}]}]}]}, {"types", ["locality", "political"]}]]}, {"status", "OK"}]}
  </code></pre>
  
  <p>I'd like to extract the latitude and the longitude from this nested data structure. I've tried using pattern matching but as the structure is fairly complex the corresponding pattern is a bit of a nightmare. While the following works it can't possibly be a good solution.</p>
  
  <pre><code>{_, [{_, [[_, _, { _, [{_, [{_, [{_, lat}, {_, lgn}]}, _]}, _, _, _] }, _]]}, _]} = json
  </code></pre>
  
  <p>So my question is what is the best way to extract values from deeply nested data structures in Elixir? Any solutions or nudges in the right direction would be greatly appreciated.</p>