Skip to content

Instantly share code, notes, and snippets.

@soffes
Created November 4, 2023 18:04
Show Gist options
  • Save soffes/947427c762d7110234a8e254fe0aa06b to your computer and use it in GitHub Desktop.
Save soffes/947427c762d7110234a8e254fe0aa06b to your computer and use it in GitHub Desktop.
Convert Amazon orders export JSON to CSV
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