skip to Main Content

I’ve been pulling data from an API in JSON, and am currently stumbling over an elmementary problem

The data is on companies, like Google and Facebook, and is in an array or hashes, like so:

[
  {"id"=>"1", "properties"=>{"name"=>"Google", "stock_symbol"=>GOOG, "primary_role"=>"company"}},
  {"id"=>"2", "properties"=>{"name"=>"Facebook", "stock_symbol"=>FB, "primary_role"=>"company"}}
]

Below are two operations I’d like to try:

  1. For each company, print out the name, ID, and the stock symbol (i.e. “Google – 1 – GOOG” and “Facebook – 2 – FB”)
  2. Remove “primary role” key/value from Google and Facebook
  3. Assign a new “industry” key/value for Google and Facebook

Any ideas?

Am a beginner in Ruby, but running into issues with some functions / methods (e.g. undefined method) for arrays and hashes as this looks to be an array OF hashes

Thank you!

2

Answers


  1. Ruby provides a couple of tools to help us comprehend arrays, hashes, and nested mixtures of both.

    Assuming your data looks like this (I’ve added quotes around GOOG and FB):

    data = [
      {"id"=>"1", "properties"=>{"name"=>"Google", "stock_symbol"=>"GOOG", "primary_role"=>"company"}},
      {"id"=>"2", "properties"=>{"name"=>"Facebook", "stock_symbol"=>"FB", "primary_role"=>"company"}}
    ]
    

    You can iterate over the array using each, e.g.:

    data.each do |result|
       puts result["id"]
    end
    

    Digging into a hash and printing the result can be done in a couple of ways:

    data.each do |result|
      # method 1
      puts result["properties"]["name"]
    
      # method 2  
      puts result.dig("properties", "name")
    end
    

    Method #1 uses the hash[key] syntax, and because the first hash value is another hash, it can be chained to get the result you’re after. The drawback of this approach is that if you have a missing properties key on one of your results, you’ll get an error.

    Method #2 uses dig, which accepts the nested keys as arguments (in order). It’ll dig down into the nested hashes and pull out the value, but if any step is missing, it will return nil which can be a bit safer if you’re handling data from an external source

    Removing elements from a hash

    Your second question is a little more involved. You’ve got two options:

    1. Remove the primary_role keys from the nested hashes, or
    2. Create a new object which contains all the data except the primary_role keys.

    I’d generally go for the latter, and recommend reading up on immutability and immutable data structures.

    However, to achieve [1] you can do an in-place delete of the key:

    data.each do |company| 
      company["properties"].delete("primary_role")
    end
    

    Adding elements to a hash

    You assign new hash values simply with hash[key] = value, so you can set the industry with something like:

    data.each do |company| 
      company["properties"]["industry"] = "Advertising/Privacy Invasion"
    end
    

    which would leave you with something like:

    [
      {
        "id"=>"1", 
        "properties"=>{
          "name"=>"Google", 
          "stock_symbol"=>"GOOG", 
          "industry"=>"Advertising/Privacy Invasion"
        }
      },
      {
        "id"=>"2", 
        "properties"=>{
          "name"=>"Facebook", 
          "stock_symbol"=>"FB", 
          "industry"=>"Advertising/Privacy Invasion"
        }
      }
    ]
    
    Login or Signup to reply.
  2. To achieve the first operation, you can iterate through the array of companies and access the relevant information for each company. Here’s an example in Ruby:

    companies = [  {"id"=>"1", "properties"=>{"name"=>"Google", "stock_symbol"=>"GOOG", "primary_role"=>"company"}},  {"id"=>"2", "properties"=>{"name"=>"Facebook", "stock_symbol"=>"FB", "primary_role"=>"company"}}]
    
    companies.each do |company|
      name = company['properties']['name']
      id = company['id']
      stock_symbol = company['properties']['stock_symbol']
      puts "#{name} - #{id} - #{stock_symbol}"
    end
    

    This will print out the name, ID, and stock symbol for each company.

    To remove the "primary role" key/value, you can use the delete method on the properties hash. For example:

    companies.each do |company|
      company['properties'].delete('primary_role')
    end
    

    To add a new "industry" key/value, you can use the []= operator to add a new key/value pair to the properties hash. For example:

    companies.each do |company|
      company['properties']['industry'] = 'Technology'
    end
    

    This will add a new key/value pair with the key "industry" and the value "Technology" to the properties hash for each company.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search