Solved

How to extract values from string in Ruby on Rails

Posted on 2014-01-18
3
403 Views
Last Modified: 2014-01-21
I'm a novice trying to extract the latitude and longitude values (under geometry > location) from the attached JSON.

I tried to use this legacy code without success:
   
    results_array = ActiveSupport::JSON.decode response.body

    if results_array && "OK" == results_array["status"]      
      results_array["results"].each do |result|
        puts logger.info lat = result["geometry"]["location"]["lat"]
   
    end

Open in new window


I figured out how to convert to key-value pairs with:
response_2 = JSON.parse(response.body)

Open in new window

...But still couldn't figure out how to extract the latitude and longitude.

I thought this would be obvious, but didn't find the solution through googling. Thanks a lot for your help.
JSON-text.txt
JSON-text-as-key-value-pairs.txt
0
Comment
Question by:bishop3000
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
Andrew Doades earned 500 total points
ID: 39791993
0
 

Author Comment

by:bishop3000
ID: 39798231
Thanks doades, the link got me on the right track.

Here's what I ended up with, which works.

  def get_site_geocoordinates  # Bishop 2014.01.18
    
    geocoder_url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address="

    url = geocoder_url + ERB::Util.url_encode( params[:site_address] )
    uri = URI.parse(url)

    response = Net::HTTP.get_response(uri)
    
    geo_text = JSON.parse(response.body)
    


    response_text = ""

    if geo_text && "OK" == geo_text["status"]
      geo_text["results"].each do |result|
        response_text = "Latitude,\"#{result["geometry"]["location"]["lat"]}\"\n"   # entry["geometry"]["location"]["lat"].to_s
        response_text += "Longitude,\"#{result["geometry"]["location"]["lng"]}\"\n"
      end
    else
      response_text = "SiteAddress,\"0\"\n"
    end


    render :text => response_text, :content_type => "text/plain" 
  
  end

Open in new window

0
 
LVL 10

Expert Comment

by:Andrew Doades
ID: 39798237
You're welcome, glad it helped you out :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question