Reputation: 97
Morning,
I am trying to create a function within rails to merge 3 files, I have 2 CSV files and 1 JSON file, These files all relate to books, The 3 files have different parts of data for the same books, so one CSV has a title and ID, the other has a different title with the same ID and a DOI, The JSON file has a hash with the authors name as the key and the value is an array of all the DOIs for the books.
Example data:
CSV One:
DOI,Title,ID
1111,Ruby on Rails,1234-1234
2222,Rails 4.0,4321-4321
CSV Two:
Title,ID
Ruby,1234-1234
Rails,4321-4321
JSON:
[{"name":"Homer Simpson","articles":["1111","2222"]}]
Example output (as JSON),
[{
"doi": "1111",
"title": "Ruby on Rails",
"author": "Homer Simpson",
"book": "Ruby",
"ID": "1234-1234"
}
{
"doi": "2222",
"title": "Rails 4.0",
"author": "Homer Simpson",
"book": "Rails",
"ID": "4321-4321"
}]
I know I need to parse the data initially, convert the CSV to array and then iterate through them, the bit i'm struggling with is the merging into a single hash.
Upvotes: 2
Views: 990
Reputation: 7612
require 'csv'
require 'json'
csv1 = CSV.open('csv1.csv').read
csv2 = CSV.open('csv2.csv').read
csv1.shift
csv2.shift
@json = JSON.parse('[{"name":"Homer Simpson","articles":["1111","2222"]}]')
@csv1_data = csv1.inject([]) do |res, el|
res << { doi: el[0], title: el[1], id: el[2] }
res
end
@csv2_data = csv2.inject([]) do |res, el|
res << { book: el[0], id: el[1] }
res
end
def book_attributes(id)
@csv2_data.find { |el| el[:id] == id }
end
def author_attributes(doi)
item = @json.find { |el| el['articles'].include? doi }
{ author: item['name'] }
end
result = @csv1_data.inject([]) do |res, el|
details = el.merge(author_attributes(el[:doi]))
details.merge!(book_attributes(el[:id]))
res << details
res
end
puts result
Hope this will do.
Upvotes: 1