Hello All,

I am trying to parse a JSON response from a Google API and am having a bit of trouble figuring out what is the right syntax to get the correct field values.  The program flow is to call the API then map the responses into my variables.  So the call looks like this: 
<$geocoder = Geo::Coder::Google->new(apiver => 3); eval { $latlong = $geocoder->geocode(location => $location); print Dumper($latlong); }; $map_coords = $latlong->{geometry}{location}{lat} . " +" . $latlong->{geometry}{location}{lng}; $country = $latlong->{address_components}{types}{count +ry}{long_name};

My problem is in the mapping for $COUNTRY.  In the JSON response, which dumps as follows, I get an error in trying to grab the long_name of the country out of the types (which I thought was a hash, but according to the error, it isn't):
$VAR1 = { 'formatted_address' => 'Johannesburg, South Africa', 'types' => [ 'locality', 'political' ], 'address_components' => [ { 'types' => [ 'locality', 'political' ], 'short_name' => 'Johannesburg', 'long_name' => 'Johannesburg' }, { 'types' => [ 'administrative_are +a_level_3', 'political' ], 'short_name' => 'Johannesburg', 'long_name' => 'Johannesburg' }, { 'types' => [ 'administrative_are +a_level_2', 'political' ], 'short_name' => 'City of Johanne +sburg Metropolitan Municipality', 'long_name' => 'City of Johannes +burg Metropolitan Municipality' }, { 'types' => [ 'administrative_are +a_level_1', 'political' ], 'short_name' => 'GP', 'long_name' => 'Gauteng' }, { 'types' => [ 'country', 'political' ], 'short_name' => 'ZA', 'long_name' => 'South Africa' } ], 'geometry' => { 'viewport' => { 'southwest' => { 'lat' => '- +26.2389231', 'lng' => '2 +7.942449' }, 'northeast' => { 'lat' => '- +26.1041199', 'lng' => '2 +8.1376001' } }, 'location' => { 'lat' => '-26.2041028', 'lng' => '28.0473051' }, 'location_type' => 'APPROXIMATE', 'bounds' => { 'southwest' => { 'lat' => '-26 +.2389231', 'lng' => '27. +942449' }, 'northeast' => { 'lat' => '-26 +.1041199', 'lng' => '28. +1376001' } } } }; Not a HASH reference at cvlt_jobs_2 line 239.
If the correct answer is 'South Africa' (address_components >> types >> country >> long_name), how do I grab that? 

In reply to Parsing a JSON response by mcoblentz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.