Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CSV output from a JSON API with a call limit
require 'httparty'
require 'csv'
require 'json'
x = 0
while x < 46296 # this is the last record in my particular API
puts "on offset #{x}"
url = "http://api.dataatwork.org/v1/jobs?offset=#{x}&limit=500"
response = HTTParty.get(url)
jobs_only = response.parsed_response.select { |job| job["uuid"] != nil }
csv_string = CSV.generate do |csv|
jobs_only.each do |hash|
csv << hash.values
end
end
File.open("jobs.csv", "a") {|f| f.write(csv_string) }
x += 500
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.