title: Automatically cast JSON coded data in ruby
body: <p>My challenge is that in the database, JSON code was untidily stored.</p>
  <pre><code>{'isr_comment':'Test Comment',
  'isr_depression_1': '1',
  'isr_depression_2': '1'
  'isr_depression_3': '1'
  'isr_tested': 'true'
  }
  </code></pre>
  <p>You see, all values are defined as string but some should be integers. It would be the best to have clean data already in the database but I cannot control how the data is entered. However my model looks like this.</p>
  <pre><code>class SessionPart &lt; ApplicationRecord
  ...
    serialize :answers, JSON
  ...
  end
  </code></pre>
  <p>As expected after deserialization is done I get strings as well.</p>
  <pre><code>@data=
    {&quot;isr_Comment&quot;=&gt;&quot;Test Comment&quot;,
     &quot;isr_depression_1&quot;=&gt;&quot;1&quot;,
     &quot;isr_depression_2&quot;=&gt;&quot;1&quot;,
     &quot;isr_depression_3&quot;=&gt;&quot;1&quot;,
     &quot;isr_tested&quot; =&gt; &quot;true&quot;}
  </code></pre>
  <p>But I need to do some calculation with this data so I need all possible values with a meaningful type.</p>
  <pre><code>@data=
    {&quot;isr_Comment&quot;=&gt;&quot;Test Comment&quot;,
     &quot;isr_depression_1&quot;=&gt;1,
     &quot;isr_depression_2&quot;=&gt;1,
     &quot;isr_depression_3&quot;=&gt;1,
     &quot;isr_tested&quot; =&gt; true}
  </code></pre>
  <p>Is there any way to cast such data automatically?</p>