I found some code here so I post it again for you so it is easy to copy.
def underscore_key(k)
if defined? Rails
k.to_s.underscore.to_sym
else
to_snake_case(k.to_s).to_sym
end
end
def to_snake_case(string)
string.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
def convert_hash_keys(value)
case value
when Array
value.map { |v| convert_hash_keys(v) }
# or `value.map(&method(:convert_hash_keys))`
when Hash
Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }]
else
value
end
end
here are some small tests to prove the functionality:
p convert_hash_keys({abc:"x"}) # => {:abc=>"x"}
p convert_hash_keys({abcDef:"x"}) # => {:abc_def=>"x"}
p convert_hash_keys({AbcDef:"x"}) # => {:abc_def=>"x"}
p convert_hash_keys(["abc"]) # => ["abc"]
p convert_hash_keys([abc:"x"]) # => [{:abc=>"x"}]
p convert_hash_keys([abcDef:"x"]) # => [{:abc_def=>"x"}]
I hope that meets your requirements.