Created
November 4, 2023 18:04
-
-
Save soffes/947427c762d7110234a8e254fe0aa06b to your computer and use it in GitHub Desktop.
Convert Amazon orders export JSON to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'csv' | |
# There are more, but these are the ones I care about | |
keys = [ | |
'Order Date', | |
'Order ID', | |
'Currency', | |
'Unit Price', | |
'Total Owed', | |
'ASIN', | |
'Quantity', | |
'Product Name' | |
] | |
files = [ | |
'Retail.OrderHistory.1.json', | |
'Retail.OrderHistory.2.json' # International orders | |
] | |
orders = files.map { |f| JSON.load(File.read(f)) }.flatten.sort { |a, b| b['Order Date'] <=> a['Order Date'] } | |
orders.each { |order| order['Order Date'].sub!('T', ' ').sub!(/(?:\.\d{3})?Z/, '') } | |
CSV.open('orders.csv', 'wb') do |csv| | |
csv << keys | |
orders.each do |order| | |
csv << keys.map { |k| order[k] } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment