How to change hash keys from `Symbol`s to `String`s?

Ruby on-RailsRubyRuby on-Rails-3Hash

Ruby on-Rails Problem Overview


I am using Ruby on Rails 3.2.2 and I would like to "easily" / "quickly" change hash keys from Symbols to Strings. That is, from {:one => "Value 1", :two => "Value 2", ...} to {"one" => "Value 1", "two" => "Value 2", ...}.

How can I make that by using less code as possible?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

simply call stringify_keys (or stringify_keys!)

http://apidock.com/rails/Hash/stringify_keys

Solution 2 - Ruby on-Rails

Use stringify_keys/stringify_keys! methods of the Hash class.

You can also use some_hash.with_indifferent_access to return a Hash instance where your key can be specified as symbols or as strings with no difference.

Solution 3 - Ruby on-Rails

stringify_keys is nice, but only available in Rails. Here's how I would do it in a single line, with zero dependencies:

new_hash = Hash[your_hash.collect{|k,v| [k.to_s, v]}]

This works on Ruby 1.8.7 and up. If you are working with Ruby 2.1, you can do:

new_hash = a.collect{|k,v| [k.to_s, v]}.to_h

Note that this solution is not recursive, nor will it handle "duplicate" keys properly. eg. if you have :key and also "key" as keys in your hash, the last one will take precedence and overwrite the first one.

Solution 4 - Ruby on-Rails

hash = hash.transform_keys(&:to_s) turns all keys from symbols into strings.

More here: https://ruby-doc.org/core-2.6.3/Hash.html#method-i-transform_keys

This was added in ruby 2.5: https://bugs.ruby-lang.org/issues/13583

Solution 5 - Ruby on-Rails

stringify_keys from rails

http://api.rubyonrails.org/classes/Hash.html#method-i-stringify_keys

hash = { name: 'Rob', age: '28' }
hash.stringify_keys
# => { "name" => "Rob", "age" => "28" }

Solution 6 - Ruby on-Rails

there is a nice library that does the trick, the library is "facets/hash/rekey" and the method is rekey!. Se my example below of how to use it. It is just a copy past of

> require 'facets/hash/rekey'
 => true
> a = {:one => "Value 1", :two => "Value 2"}
 => {:one=>"Value 1", :two=>"Value 2"} 
> a.rekey!(&:to_s)
 => {"one"=>"Value 1", "two"=>"Value 2"} 
> a
 => {"one"=>"Value 1", "two"=>"Value 2"}

Solution 7 - Ruby on-Rails

 new_hash = Hash.new
 your_hash.each{ |k,v| new_hash[k.to_s] = v }

new_hash will be same as your_hash but with string keys

Solution 8 - Ruby on-Rails

I came here to see if there was something better than:

JSON.parse(hash.to_json)

But I think I'll stick with what I have.

Solution 9 - Ruby on-Rails

You can transfer the key from symbols to strings explicitly:
hash = hash.map { |k, v| [k.to_s, v] }.to_h

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser12882View Question on Stackoverflow
Solution 1 - Ruby on-RailsViktor TrónView Answer on Stackoverflow
Solution 2 - Ruby on-RailsjdoeView Answer on Stackoverflow
Solution 3 - Ruby on-RailseliView Answer on Stackoverflow
Solution 4 - Ruby on-RailsmooredsView Answer on Stackoverflow
Solution 5 - Ruby on-RailsEryView Answer on Stackoverflow
Solution 6 - Ruby on-Railsrik.vanmechelenView Answer on Stackoverflow
Solution 7 - Ruby on-RailsabhasView Answer on Stackoverflow
Solution 8 - Ruby on-RailspguardiarioView Answer on Stackoverflow
Solution 9 - Ruby on-RailskjianView Answer on Stackoverflow