Skip to content

Instantly share code, notes, and snippets.

@emailrhoads
Last active March 30, 2020 21:28
Show Gist options
  • Select an option

  • Save emailrhoads/70c91bb36bc58b3ed252f0010c7ceeaf to your computer and use it in GitHub Desktop.

Select an option

Save emailrhoads/70c91bb36bc58b3ed252f0010c7ceeaf to your computer and use it in GitHub Desktop.
[Load Ruby Hash to CSV into Postgres] #postgres

Hash to CSV

my_json = JSON.parse(File.open("ss_to_census_100k_all_unique_results.json").read);
csv_string = CSV.generate(headers: true) do |csv|
  csv << my_json.first.keys
  my_json.each do |hash|
    csv << hash.values
  end
end;
File.write("ss_to_census_100k_all_unique_results.csv", csv_string)

Create Table

CREATE table results_ss_census_all(
  loan_number varchar,
  msa_identifier varchar,
  tract_income_category varchar,
  application_income_category varchar,
  county_name varchar,
  normalized_address_line_text varchar,
  normalized_city_name varchar,
  normalized_property_postal_code varchar,
  normalized_property_state varchar,
  error_desc varchar,
  census_trac varchar,
  appl_inc_perc_of_median double precision,
  tract_inc_perc_of_median double precision,
  total_monthly_income double precision
);

CSV into PG

COPY results_ss_census_all FROM '/home/john/cmit/crape/ss_to_census_100k_all_unique_results.csv' DELIMITER ',' CSV HEADER;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment