title: Trouble handling HTTP responses and parsing JSON data
body: <p>I am using Ruby on Rails 3 and I would like to solve an issue with the following code where a web client application receive back some JSON data from a web service application that uses a Rack middleware in order to respond.</p>
  
  <p>In the web client app model I have</p>
  
  <pre><code>response_parsed = JSON.parse(response.body)
  if response_parsed["account"]
    ...
  else
    return response
  end
  </code></pre>
  
  <p>In the above code the <code>response.body</code> come back from the web service app that uses a Rack middleware to respond to the web client:</p>
  
  <pre><code>accounts = Account.where(:id =&gt; ids)
  [200, {'Content-Type' =&gt; 'application/json'}, accounts.to_json] # That is, response.body = accounts.to_json
  </code></pre>
  
  <p>Data transmission is ok, but I get the following error</p>
  
  <pre><code>TypeError
  can't convert String into Integer
  
  *Application Trace* 
  lib/accounts.rb:107:in `[]'
  </code></pre>
  
  <p>The line <code>107</code> corresponds to </p>
  
  <pre><code>if response_parsed["account"]
    ...
  </code></pre>
  
  <p><strong>Where and what is the problem? How to solve that?</strong></p>
  
  <hr>
  
  <p>If I try to debug the <code>respons.body</code> I get</p>
  
  <pre><code># Note: this is an array!
  "[{\"account\":{\"firstname\":\"Semio\",\"lastname\":\"Iaven\"\"}}]"
  </code></pre>