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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:29:32+00:00 2026-06-11T04:29:32+00:00

I have a hash data like this: { current_condition => [ { cloudcover =>

  • 0

I have a hash data like this:

{
  "current_condition" => [
    {
      "cloudcover"       => "100",
      "humidity"         => "100",
      "observation_time" => "05:44 AM",
      "precipMM"         => "0.0",
      "pressure"         => "1015",
      "temp_C"           => "14",
      "temp_F"           => "57",
      "visibility"       => "13",
      "weatherCode"      => "122",
      "weatherDesc"      => [
        {
          "value" => "Overcast"
        }
      ],
      "weatherIconUrl" => [
        {
          "value" => "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"
        }
      ],
      "winddir16Point" => "NNW",
      "winddirDegree"  => "340",
      "windspeedKmph"  => "15",
      "windspeedMiles" => "9"
    }
  ], 
  "request" => [
    {
      "query" => "94127",
      "type"  => "Zipcode"
    }
  ],
  "weather" => [
    {
      "date"        => "2012-09-09",
      "precipMM"    => "0.0",
      "tempMaxC"    => "21",
      "tempMaxF"    => "69",
      "tempMinC"    => "12",
      "tempMinF"    => "53",
      "weatherCode" => "113",
      "weatherDesc" => [
        {
          "value" => "Sunny"
        }
      ],
      "weatherIconUrl" => [
        {
          "value" => "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
        }
      ],
      "winddir16Point" => "W",
      "winddirDegree"  => "279",
      "winddirection"  => "W",
      "windspeedKmph"  => "23",
      "windspeedMiles" => "14"
    },
    {
      "date"        => "2012-09-10",
      "precipMM"    => "0.1",
      "tempMaxC"    => "20",
      "tempMaxF"    => "68",
      "tempMinC"    => "12",
      "tempMinF"    => "53",
      "weatherCode" => "119",
      "weatherDesc" => [
        {
          "value" => "Cloudy"
        }
      ],
      "weatherIconUrl" => [
        {
          "value" => "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"
        }
      ],
      "winddir16Point" => "WSW",
      "winddirDegree"  => "252",
      "winddirection"  => "WSW",
      "windspeedKmph"  => "17",
      "windspeedMiles" => "11"
    }
  ]
}

Some of the hash values are just strings, some of them are arrays with only one element like this:

"weatherDesc"=>[{"value"=>"Cloudy"}]

I want to make all the elements like this in the hash to be like:

"weatherDesc"=>{"value"=>"Cloudy"}

Is there an easy Ruby method, or a single loop that can do this? Or do I need to loop through each key-val pair to flatten this?

–update -9-11-2012

Thanks for those who discussed and helped me out. Here’s an update, I just found that there’s actually one hash value has 2 objects in the array, I modified the @iioiooioo ‘s code in this line

hash[k] = v.first if v.is_a?( Array ) && v.count == 1

–more update on this, that above doesn’t work correctly since there are arrays in the array who doesn’t get cleaned since the array with 2 elements is not processed, which would end the recursion on it. I end up doing this, which is not pretty but…

def arr_to_hash(a)
  hash = {}
  for i in 0..a.length-1
      hash[i.to_s] = a[i]
  end
  hash
end

def clean_it( hash )
  hash.each do |k,v|
  hash[k] = arr_to_hash v if v.is_a?( Array ) && v.count > 1
  hash[k] = v.first if v.is_a?( Array ) && v.count == 1
  clean_it( hash[k] ) if hash[k].is_a?( Hash )
  end
end
  • 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-11T04:29:33+00:00Added an answer on June 11, 2026 at 4:29 am

    I do not believe a simple loop will suffice, I think you need to use recursion, like so:

    a = {"current_condition"=> [{"cloudcover"=>"100", "humidity"=>"100", "observation_time"=>"05:44 AM", "precipMM"=>"0.0", "pressure"=>"1015", "temp_C"=>"14", "temp_F"=>"57", "visibility"=>"13", "weatherCode"=>"122", "weatherDesc"=>[{"value"=>"Overcast"}], "weatherIconUrl"=>[{"value"=>"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"}], "winddir16Point"=>"NNW", "winddirDegree"=>"340", "windspeedKmph"=>"15", "windspeedMiles"=>"9"}], "request"=>[{"query"=>"94127", "type"=>"Zipcode"}], "weather"=>[{"date"=>"2012-09-09", "precipMM"=>"0.0", "tempMaxC"=>"21", "tempMaxF"=>"69", "tempMinC"=>"12", "tempMinF"=>"53", "weatherCode"=>"113", "weatherDesc"=>[{"value"=>"Sunny"}], "weatherIconUrl"=>[{"value"=>"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"}], "winddir16Point"=>"W", "winddirDegree"=>"279", "winddirection"=>"W", "windspeedKmph"=>"23", "windspeedMiles"=>"14"}, {"date"=>"2012-09-10", "precipMM"=>"0.1", "tempMaxC"=>"20", "tempMaxF"=>"68", "tempMinC"=>"12", "tempMinF"=>"53", "weatherCode"=>"119", "weatherDesc"=>[{"value"=>"Cloudy"}], "weatherIconUrl"=>[{"value"=>"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"}], "winddir16Point"=>"WSW", "winddirDegree"=>"252", "winddirection"=>"WSW", "windspeedKmph"=>"17", "windspeedMiles"=>"11"}]}
    
    
    def clean_it( hash )
    
     hash.each do |k,v|
      hash[k] = v.first if v.is_a?( Array )
      clean_it( hash[k] ) if hash[k].is_a?( Hash )
     end
    
    end
    
    clean_it( a )
    
    p a
    

    Outputs:

    {"current_condition"=>{"cloudcover"=>"100", "humidity"=>"100", "observation_time"=>"05:44 AM", "precipMM"=>"0.0", "pressure"=>"1015", "temp_C"=>"14", "temp_F"=>"57", "visibility"=>"13", "weatherCode"=
    >"122", "weatherDesc"=>{"value"=>"Overcast"}, "weatherIconUrl"=>{"value"=>"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"}, "winddir16Point"=>"NNW", "wind
    dirDegree"=>"340", "windspeedKmph"=>"15", "windspeedMiles"=>"9"}, "request"=>{"query"=>"94127", "type"=>"Zipcode"}, "weather"=>{"date"=>"2012-09-09", "precipMM"=>"0.0", "tempMaxC"=>"21", "tempMaxF"=>"
    69", "tempMinC"=>"12", "tempMinF"=>"53", "weatherCode"=>"113", "weatherDesc"=>{"value"=>"Sunny"}, "weatherIconUrl"=>{"value"=>"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_s
    unny.png"}, "winddir16Point"=>"W", "winddirDegree"=>"279", "winddirection"=>"W", "windspeedKmph"=>"23", "windspeedMiles"=>"14"}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Hash like this, representing a data tree hash = { 'key1' =>
I have a code like this: use Data::Dumper; my %hash = ( 'chrX' =>
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 data that looks like this: id:40108689 -- chr22_scrambled_bysegments:10762459:F : chr22:17852459:F (1.0),
I have a has that looks like this: data = {abc -> [[date1, val1],
I have a function that generates a MD5 hash in C# like this: MD5
I have a data structure containing a list of objects, like this: class A
I have little problem with printing data like this, I have written script like
I have a Perl data structure like the following: %hash = ( table_id =>

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.