Ruby JSON parse changes Hash keys Ruby JSON parse changes Hash keys ruby ruby

Ruby JSON parse changes Hash keys


The JSON generator converts symbols to strings because JSON does not support symbols. Since JSON keys are all strings, parsing a JSON document will produce a Ruby hash with string keys by default.

You can tell the parser to use symbols instead of strings by using the symbolize_names option.

Example:

original_hash = {:info => [{:from => "Ryan Bates", :message => "sup bra", :time => "04:35 AM"}]}serialized = JSON.generate(original_hash)new_hash = JSON.parse(serialized, {:symbolize_names => true})new_hash[:info] #=> [{:from=>"Ryan Bates", :message=>"sup bra", :time=>"04:35 AM"}]

Reference: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html#method-i-parse


In short, no. Think about it this way, storing symbols in JSON is the same as storing strings in JSON. So you cannot possibly distinguish between the two when it comes to parsing the JSON string. You can of course convert the string keys back into symbols, or in fact even build a class to interact with JSON which does this automagically, but I would recommend just using strings.

But, just for the sake of it, here are the answers to this question the previous times it's been asked:

what is the best way to convert a json formatted key value pair to ruby hash with symbol as key?

ActiveSupport::JSON decode hash losing symbols

Or perhaps a HashWithIndifferentAccess


I solved my similar issue with calling the with_indifferent_access method on it

Here I have a json string and we can assign it to variable s

s = "{\"foo\":{\"bar\":\"cool\"}}"

So now I can parse the data with the JSON class and assign it to h

h = JSON.parse(s).with_indifferent_access

This will produce a hash that can accept a string or a symbol as the key

h[:foo]["bar"]  #=> "cool"