title: Rails jsonb - Prevent JSON keys from reordering when jsonb is saved to Postgresql database
body: <p>I have a column <code>amount_splits</code> that I need to save my JSON to in the key order I've specified.</p>
  
  <p>How do I prevent Rails / Postgres <code>jsonb</code> from auto sorting my JSON keys when I save it to the database? (for creating or updating)</p>
  
  <p>It looks like it's <em>trying</em> to sort alphabetically, but does a poor job at it.</p>
  
  <h1>Here's what I'm saving:</h1>
  
  <pre><code>{
      "str_fee": 3.17,       # key 1
      "eva_fee": 14.37,      # key 2
      "fran_royalty": 14.37, # key 3
      "fran_amount": 67.09   # key 4
  }
  </code></pre>
  
  <h1>This is how it actually saves:</h1>
  
  <pre><code>{
      "eva_fee": 14.37,     # key 2
      "str_fee": 3.17,      # key 1
      "fran_amount": 67.09, # key 4
      "fran_royalty": 14.37 # key 3
  }
  </code></pre>
  
  <h1>Purpose:</h1>
  
  <p><em>Before you answer "sorting doesn't matter when the JSON is consumed on the receiving end", stop and think first please... and please read on</em></p>
  
  <p><strong>I need the keys to be sorted in the way I need them sorted because</strong> the client interface that consumes this JSON is displaying the JSON to developers that need the keys to be in the order that the documentation tells them its in. And the reason it needs to be in that order is to display the process of what calculations happened in which order first:</p>
  
  <p><strong>The correct order tells the developer:</strong></p>
  
  <p>The <code>str_fee</code> was applied first, then the <code>eva_fee</code>, then the <code>fran_royalty</code>... making <code>fran_amount</code> the ending amount.</p>
  
  <p><strong>But based on how <code>jsonb</code> sorts this, it incorrectly tells our developers that:</strong></p>
  
  <p>The <code>eva_fee</code> was applied first, then the <code>str_fee</code>, then the <code>fran_amount</code>... making <code>fran_royalty</code> the ending amount.</p>