My controller receives a JSON string inside params which looks like following:
{"serialized"=>"{\"key\":{\"subkey1\":"value",\"subkey2\":"value"}"}
In my controller I try the following:
JSON.parse(params[:serialized], symbolize_names: true)
Which returns:
{:"key"=>{:subkey1=>"value", :subkey2=>"value"}
All the nested subkeys were symbolized; the key was symbolized in a weird way so it is not responding to hash[key], but does to hash["key"].
If I go through the Rails stack:
ActiveSupport::JSON.decode(params[:serialized]).symbolize_keys
I get back the following:
{:"key"=>{"subkey1"=>"value", "subkey2"=>"value"}
Almost same as the first one except for nested keys; they are not being symbolized.
I've even tried looping through hash trying to symbolize the keys manually; with no success though:
Hash[params[:serialized]{ |k, v| [k.to_sym, v] }] # returns {:"key"=>{"subkey1"=>"value", "subkey2"=>"value2"}
Why is this happening? Why is the key symbolized as :"key" instead of :key?
UPD Removed last line (How could I possibly fix that since I need my hash to answer to hash[key] and not hash["key"].) so the question looks less pragmatic and more theoretic.
:key == :"key" #=> truehash[key]instead ofhash[:key]to be possible?