Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6977949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:41:32+00:00 2026-05-27T17:41:32+00:00

I need to create a map/array for auto complete from a JSON response and

  • 0

I need to create a map/array for auto complete from a JSON response and I am looking for the best, most efficient way to do it in Ruby and Rails 3. A portion of the response is below and the working code I have is before it. What is the one line of code I need to create locations for me?

# Need help making this more efficient
response_fields = JSON.parse(response.body)
predictions = response_fields['predictions']
predictions.each do |prediction|
  locations << prediction['description']
end

Sample response from API:

{
   "predictions" : [
     {
       "description" : "Napa, CA, United States",
       "id" : "cf268f9fb9a1b46aed72d59ab85ed40f982763c6",
       "matched_substrings" : [
          {
            "length" : 4,
            "offset" : 0
          }
       ],
     "reference" : "CjQvAAAAqZWNGzqtJf3awNuQNQdnZpl4dBVVXFPrPdz29r1jo1GMWYFuz3KRlK9HgdgszOThEhDeYz_vYgcOPJTaYehF11bUGhR8yH9zqMGV9kenZIo9OTBrSwftgg",
     "terms" : [
        {
           "offset" : 0,
           "value" : "Napa"
        },
        {
           "offset" : 6,
           "value" : "CA"
        },
        {
           "offset" : 10,
           "value" : "United States"
        }
     ],
     "types" : [ "locality", "political", "geocode" ]
  },
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:41:33+00:00Added an answer on May 27, 2026 at 5:41 pm

    You can shorten your code like this:

    locations = JSON.parse(response.body)['predictions'].map { |p| p['description'] }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a map, from integers to sets of tuples, the tuples
I need to create an array of arrays. I have been using array_map(null,$a,$b,$c) to
In data processing, I frequently need to create a lookup data structure to map
i need create an email list sending to many emails. what is best solution
I need to create a historical timeline starting from 1600's to the present day.
I'm struggling to create an array of ints in objective c. the way I'd
I'm trying to create a json object from MySQL results, but not getting the
I need to create 3000x3000 bit map for coords of my robots. In theory
I need to create a hash map which will hold 528 bit binary data.
I need create custom dialog and put JPanel into it. Is it possible?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.