I'm currently trying to sync my database up with another database with Json, but I'm running into issues with a key they're using.
def sync
url = '...' # Returns "{ "class": 20, "user": "John" }"
uri = URI(url)
response = Net::HTTP.get(uri)
response_hash = JSON.parse(response)
...
They use the keyword 'class' in their response, but I don't think I'm allowed that, so I have mine as 'cls'. Now, if I try to create a new User, I'll get an error because it cannot map 'class' to anything.
I've been trying to change the key to match my new key, but have been unable to get that to work.
Using answers such as has no effect for some reason.
pattern = { "class" => "cls" }
response_hash.inject({}) do |new_hash, (k, v)|
key = pattern[k] || k
new_hash[key] = v
new_hash
end
But I did notice that I have a Hash, whereas some of the examples created a HashWithIndifferentAccess, so they could do response_hash[:class], but mine would only return nil.
What would be the best solution for this problem? Ideally, I wouldn't have to remap any keys, and could just throw the Json into my database.
Thanks for your help.