title: Ruby API - JSON Parsing - Writing to file
body: <p>I  get the REST response data from a URL. I then write it to a JSON file, however, it's put it on one line in a long string, and I need to have it in readable format in the JSON file.</p>
  
  <p>My code is:</p>
  
  <pre><code>require 'rubygems'
  require 'json'
  require 'rest-client'
  
  class Rest
  
    def self.getData
  
      response = RestClient.get 'http://jsonplaceholder.typicode.com/posts'
      response = JSON.parse(response)
  
      File.open('/Users/robertreed/RubymineProjects/draft/posts.json', 'w') do |f|
        f.write(response.to_json)
      end
  
  
  
      puts response   
    end
  
    getData
  
  end
  </code></pre>
  
  <p>It's printing to the console and writing to the JSON file on one line:</p>
  
  <pre><code>[{"userId"=&gt;10, "id"=&gt;100, "title"=&gt;"at nam consequatur ea labore ea harum", "body"=&gt;"cupiditate quo est a modi nesciunt}]
  </code></pre>
  
  <p>Any suggestions on how I could achieve this?</p>