rails csv to json

require 'csv' 
require 'json'

 csv_table = CSV.parse(File.read(filename), headers: true,:col_sep => ';' )
 @json_string = csv_table.map(&:to_h).to_json 
 @json_string = JSON.parse(@json_string)
 @email=@json_string.map{|e| e["email"]}
 @json_string=@email print(@json_string,"san")

csv file inside

email;Identifier;password;Recovery code;Fname;Lname;Department;Location
rachel@example.com;9012;12se74;rb9012;Rachel;Booker;Sales;Manchester
laura@example.com;2070;04ap67;lg2070;Laura;Grey;Depot;London
craig@example.com;4081;30no86;cj4081;Craig;Johnson;Depot;London
mary@example.com;9346;14ju73;mj9346;Mary;Jenkins;Engineering;Manchester
jamie@example.com;5079;09ja61;js5079;Jamie;Smith;Engineering;Manchester

Leave a Comment

Your email address will not be published. Required fields are marked *