title: Iterate JSON with Ruby and insert to MySQL db
body: <p>I'm acceessing an open JSON API like this</p>
  
  <pre><code>require 'net/http'
  require 'rubygems'
  require 'json'
  require 'uri'
  require 'pp'
  
  url = "http://api.turfgame.com/v4/users"
  uri = URI.parse(url)
  
  data = [{"name" =&gt; "tbone"}]
  
  
  headers = {"Content-Type" =&gt; "application/json"}
  
  http = Net::HTTP.new(uri.host,uri.port)
  
  response = http.post(uri.path,data.to_json,headers)
  </code></pre>
  
  <p>This gives a JSON ouput like this</p>
  
  <pre><code>[{"region"=&gt;{"id"=&gt;141, "name"=&gt;"Stockholm"}, "medals"=&gt;[34, 53, 12, 5, 46], "pointsPerHour"=&gt;95, "blocktime"=&gt;24, "zones"=&gt;[275, 42460, 35956, 31926, 24247, 31722, 1097, 26104, 6072, 24283, 289, 325, 22199, 37740, 22198, 37743, 37074, 22845, 22201, 22846, 7477, 7310], "country"=&gt;"se", "id"=&gt;95195, "rank"=&gt;24, "name"=&gt;"tbone", "uniqueZonesTaken"=&gt;178, "taken"=&gt;1170, "points"=&gt;41693, "place"=&gt;377, "totalPoints"=&gt;176654}]  
  </code></pre>
  
  <p>What I want to do is to grab some of the tags: </p>
  
  <ul>
  <li>name (not in the region block but "tbone")</li>
  <li>blocktime</li>
  <li>totalPoints</li>
  <li>all the IDs from the zone-array </li>
  </ul>
  
  <p>and insert into a mysql table. But I don't get how to iterate the JSON object and get the stuff I want.</p>
  
  <p>doing </p>
  
  <pre><code>puts data["name"]
  </code></pre>
  
  <p>gives an error like</p>
  
  <pre><code>./headerTest.rb:28:in `[]': can't convert String into Integer (TypeError)
          from ./headerTest.rb:28:in `&lt;main&gt;'
  </code></pre>
  
  <p>And I get that it's because there's two name tags but at different depth but i don't get how to accees either one specifically.</p>
  
  <p>Please?</p>