I'm trying to get a Hash from a Json file that has Array Keys, but its return each array key like string.
hash = {[10, 10] => [[1, 1], [5, 5]]}
p JSON.parse(hash.to_json) #=> {"[10, 10]" => [[1, 1], [5, 5]]}
Maybe i should use YAML, any idea?
There are three slightly different versions JSON, as specified by
While there are small differences between the three, one thing they all agree on: Object Keys are Strings. Always.
In other words, "a Json file that has Array Keys" cannot possibly exist. Whatever you have, it is either a JSON file, but then it cannot have Array Keys, or it is simply not a JSON file.
#to_jsonis calling[10,10].to_sto stringify the key before encoding it to JSON. I don't think YAML will help. Marshal won't help either, that's not a good serialization format as it depends on the Ruby version. I'd guess that you're going to end up with some custom format (even if it means double encoding by calling[10,10].to_jsonto stringify the key before JSON encoding the hash, i.e. JSON inside your JSON).