title: ruby POST requested Json to structured array or hash for CSV
body: <pre><code>{
    "TOTAL" : "520"
  
    ,
  
    "PROD_101379" : {
      "IMG" : "1406301107587209.jpg",
      "NAME" : "hello sunny",
      "LINK" : "/product/productDetail.do?seq=101379",
      "SEQ" : "101379",
      "PRICE" : "18000",
      "MILEAGE" : "2"
    }
    ,
  
    "PROD_101378" : {
      "IMG" : "",
      "NAME" : "special gift",
      "LINK" : "/product/productDetail.do?seq=101378",
      "SEQ" : "101378",
      "PRICE" : "3000",
      "MILEAGE" : "2"
    }
    ,
  
    "PROD_101376" : {
      "IMG" : "1405020190326241.jpg",
      "NAME" : "it radiant",
      "LINK" : "/product/productDetail.do?seq=101376",
      "SEQ" : "101376",
      "PRICE" : "45000",
      "MILEAGE" : "2"
    }
      ,
  
    "PAGER" : "&amp;lt;div class=&amp;quot;paging&amp;quot;&amp;gt;&amp;lt;a href=&amp;quot;javascript:pageForm('0');
  
  }
  </code></pre>
  
  <p>I am learning how to program in Ruby, using Nokogiri gem to parse internet data.</p>
  
  <p>Above data was received after requesting with POST using Net/HTTP, then parsing it with Json gem. </p>
  
  <pre><code>JSON.parse(x.body)
  </code></pre>
  
  <p>I am trying to turn that data into CSV like so:</p>
  
  <pre><code>IMG | Name | Link | SEQ | Price | Mileage |
  </code></pre>
  
  <p>for each PROD_xxxxxx arrays.</p>
  
  <p>I've read the gem documentation, and looked at other questions here to realize that much like Nokogiri parsing HTML(or more like Array &amp; Hash), I should be able to parse JSON format.</p>
  
  <p>I found that I can get the value of something by:</p>
  
  <pre><code>json_parsed["TOTAL"]
  </code></pre>
  
  <p>which would give me "520"</p>
  
  <p>I get use the same approach to get at the "PROD_xxxxxx" nested data, but that would give me only specific nested data for that name.</p>
  
  <p>I would like to be able to loop through them, so I've tried something like</p>
  
  <pre><code>json_parsed["PROD*"].each do |each|
  </code></pre>
  
  <p>but looks like this is not the correct way to use the syntax.</p>
  
  <p>If can I loop through each and get "IMG", "NAME", "LINK", ...etc, I am trying to use CSV to:</p>
  
  <pre><code>CSV.open(fname,"w") do |csv|
    Json_parsed[each-PROD].each do |each|
     name = each["NAME"]
     img = each["IMG"]
     csv &lt;&lt; [name, img]
    end
  end
  </code></pre>
  
  <p>However, if there is a better way than the above approach to turn the JSON data into CSV (maybe this is possible as JSON is a structured data, like CSV?) I would appreciate the suggestion.</p>
  
  <p>Thanks,</p>