Skip to content

Instantly share code, notes, and snippets.

@yevgenko
Last active July 16, 2019 11:20
Show Gist options
  • Save yevgenko/f9b473fa2c32c5b207847178b15f8a01 to your computer and use it in GitHub Desktop.
Save yevgenko/f9b473fa2c32c5b207847178b15f8a01 to your computer and use it in GitHub Desktop.
Convert flatfile.io output (key-value map) to CSV string.
require 'csv'
require 'json'
def flatfile_io_json2csv_string(data_as_string = '[{"sku_code": "123"}, {"sku_code": "321"}]')
CSV.generate do |csv|
json_data = JSON.parse(data_as_string)
csv << json_data.first.keys # header
json_data.each do |hash|
csv << hash.values
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment