0
    "divisions":{
      "ocd-division/country:us/state:co/place:aurora":{
       "name":"Aurora city",
       "scope":"citywide",
       "officeIds":[
          "O0",
          "O1"]
    }}

I am using Google Civic Information API to get representive information. let's say this is a part of JSON generated by Google and I want to access its elements but the issue is - i cant use "ocd-division/country:us/state:co/place:aurora" as a key, because its dynamically generated by Google.

If I search for some different address like India, the key will be different from "ocd-division/country:us/state:co/place:aurora" to something else(instead of 'us', there would be 'in'), So please suggest the way I should access in this case the value.

Please clarify if you don't understand question or need more clarification.

1 Answer 1

2

Parse the json to a hash:

parsed_data = JSON.parse google_data

then either iterate over all divisions:

parsed_data['divisions'].each do |division_key, division_info|
  # do something with each division
end

or get only the first one:

division_key, division_info = parsed_data['divisions'].first

Use this if you want parse the key as well:

more_info = division_key.split('/').inject(Hash.new) do |hash, key_part|
  if key_part.include? ":" 
    key, value = key_part.split ":"
    hash[key] = value
  end
  hash
end

Now you can access it via more_info['country']

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.