-2

I am trying when I run a ruby file to generate a json file, the script is working but it just display the json on the terminal, I don't know to to create a file from it, here is what I did :

  require 'pg'

conn = PGconn.connect("host", 5432, '', '', "d6kabu5l22jugs", "cgcscdqtkpesow", "5fe2bc97a")

res  = conn.exec('select * FROM applicants')
SELECT array_to_json(array_agg(applicants)) FROM applicants;

Thanks,

Majid
  • 95
  • 1
  • 7

1 Answers1

0

What you need to do is execute the query and then write it on to a file.

require 'pg'

conn = PGconn.connect("host", 5432, '', '', "d6kabu5l22jugs", "cgcscdqtkpesow", "5fe2bc97a")

res  = conn.exec("
SELECT array_to_json(array_agg(a)) as response FROM (select * from applications) a")

once you got the result you write it to a file.

File.open("dump.json","w") do |f|
  f.write(res.first['response'])
end
coderhs
  • 4,357
  • 1
  • 16
  • 25