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 8391837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:21:50+00:00 2026-06-09T19:21:50+00:00

I have a hash in the database in JSON format. eg { one =>

  • 0

I have a hash in the database in JSON format. eg

{
  "one" => {
    "two" => {
      "three" => {}
    }
  } 
}

I need to generate this from a string. The above example would be generated from the string “one.two.three”.

Firstly how would I do this?

Second part of the problem. I will be receiving multiple strings – each one building on the last. So if I get “one.two.three”, then “one.two.four”, I hash to be this:

{
  "one" => {
    "two" => {
      "three" => {},
      "four" => {}
    }
  } 
}

And if I get “one.two.three” twice, I want the latest “three” value to override what was there. Strings can also be of any length (e.g. “one.two.three.four.five” or just “one”). Hopefully that makes sense?

  • 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-06-09T19:21:51+00:00Added an answer on June 9, 2026 at 7:21 pm

    To generate nested hash:

    hash = {}
    
    "one.two.three".split('.').reduce(hash) { |h,m| h[m] = {} }
    
    puts hash #=> {"one"=>{"two"=>{"three"=>{}}}}
    

    If you don’t have rails installed then install activesupport gem:

    gem install activesupport
    

    Then include it into your file:

    require 'active_support/core_ext/hash/deep_merge'
    
    hash = {
      "one" => {
        "two" => {
          "three" => {}
        }
      } 
    }.deep_merge(another_hash)
    

    The access to the internals would be:

    hash['one']['two']['three'] #=> {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to break down a JSON string into smaller objects. I have two
I have a hash very similar to the following JSON object; This is only
What I have is 2 hash's one gets dumped from a backend that deals
I have a hash of values from a database query: a1 = {:a =>
I have @hash that looks like this: [1, {:clid=>1, :nvz=>4, :tip=>IP, :name=>Mark, :record=>some text}]
I have a hash that looks like this h1 = {4c09a0da6071a593f051de32=>[4c09a0da6071a593f051de32, Cafe Bistro, 37.78458803130115,
I have a Hash Map (many-to-one relationship between texts and boolean values): name flag
I have intialized Hash map inside static block, I need to access the hashmap
I've read several stackoverflow posts about this topic, particularly this one: Secure hash and
I have a list of shows in a database that need to be output

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.