0

I am trying to make a localized version of this app: SMS Broadcast Ruby App

I have been able to get the JSON data from a local file & sanitize the number as well as open the JSON data. However I have been unable to extract the values and pair them as a scrubbed hash. Here's what I have so far.

  def data_from_spreadsheet
    file = open(spreadsheet_url).read
    JSON.parse(file)
  end

  def contacts_from_spreadsheet
    contacts = {}
    data_from_spreadsheet.each do |entry|
     puts entry['name']['number']
     contacts[sanitize(number)] = name
    end
    contacts
  end

Here's the JSON data sample I'm working with.

[
 {
   "name": "Michael",
   "number": 9045555555
 },
 {
   "name": "Natalie",
   "number": 7865555555
 }
]

Here's how I would like the JSON to be expressed after the contacts_from_spreadsheet method.

{
  '19045555555' => 'Michael', 
  '19045555555' => 'Natalie'
}

Any help would be much appreciated.

3
  • So you have no problem with the whole JSON part? Then why ask about it? Commented Jan 23, 2018 at 12:45
  • I don't think entry['name']['number'] is a thing in your data structure. Commented Jan 23, 2018 at 12:53
  • 3
    What's the actual error you're getting? Or what is the output from sanitize(number)? Commented Jan 23, 2018 at 12:55

3 Answers 3

1

You could create array of pairs (hashes) using map and then call reduce to get a single hash.

data = [{
    "name": "Michael",
    "number": 9045555555
},
{
    "name": "Natalie",
    "number": 7865555555
}]

data.map{|e| {e[:number] => e[:name]}}.reduce Hash.new, :merge

Result: {9045555555=>"Michael", 7865555555=>"Natalie"}

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

3 Comments

That worked! Thanks! How would I be able to make data = (file_url.json) instead of having the static array within the method?
def spreadsheet_url 'contacts.json' end' def contacts_from_spreadsheet sample_data = open(spreadsheet_url) sample_data.map{|e| {e[:number] => e[:name]}}.reduce Hash.new, :merge end
Sorry, but it is not clear enough. Could you provide question in your post and describe an issue?
0

You don't seem to have number or name extracted in any way. I think first you'll need to update your code to get those details.

i.e. If entry is a JSON object (or rather was before parsing), you can do the following:

def contacts_from_spreadsheet
  contacts = {}
    data_from_spreadsheet.each do |entry|
     contacts[sanitize(entry['number'])] = entry['name']
  end
  contacts
end

Comments

0

Not really keeping this function within JSON, but I have solved the problem. Here's what I used.

def data_from_spreadsheet
  file = open(spreadsheet_url).read
  YAML.load(file)
end

def contacts_from_spreadsheet
  contacts = {}
  data_from_spreadsheet.each do |entry|
    name = entry['name']
    number = entry['phone_number'].to_s
    contacts[sanitize(number)] = name
  end
  contacts
end

This returned back clean array here:

{"+19045555555"=>"Michael", "+17865555555"=>"Natalie"}

Thanks everyone who added input!

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.