title: Order of keys in a JSON object converted to a Ruby Hash with JSON.parse
body: <p>In Javascript, object keys are not ordered, whereas in Ruby, <a href="http://ruby-doc.org/core-1.9.3/Hash.html" rel="nofollow">Hashes enumerate their values in the order that the corresponding keys were inserted</a>.</p>
  
  <p>In practice, it seems to be the case that when converting JSON objects to Ruby Hashes, keys in the Ruby Hash are in the order in which they were written in the JSON data, e.g.:</p>
  
  <pre><code>irb(main):002:0&gt; JSON.parse('{"a": 1, "b": 2}')
  =&gt; {"a"=&gt;1, "b"=&gt;2}
  
  irb(main):005:0&gt; JSON.parse('{"b": 1, "a": 2}')
  =&gt; {"b"=&gt;1, "a"=&gt;2}
  </code></pre>
  
  <p>Can I rely on the assumption that it will always be the case?</p>
  
  <p>I'd think so, because when parsing JSON, I guess the parser is inserting keys as it discovers them, but I couldn't find any reference on the subject.</p>