Skip to content

Instantly share code, notes, and snippets.

@uxDaniel
Created January 6, 2026 06:47
Show Gist options
  • Select an option

  • Save uxDaniel/c3d4555c049d20a481e0ccf8480a905d to your computer and use it in GitHub Desktop.

Select an option

Save uxDaniel/c3d4555c049d20a481e0ccf8480a905d to your computer and use it in GitHub Desktop.
Costco receipts converter from json to csv
#!/usr/bin/env python3
# Download receipts using https://gist.github.com/dlh3/7e18fec42cc7c43d51e93a6d2bcac6fb
"""
Convert Costco JSON to CSV format
Handles nested JSON structure with receipts and item arrays
"""
import json
import csv
import os
from typing import List, Dict, Any
def flatten_receipt_items(receipt: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
Flatten receipt data with items into separate rows for each item
"""
base_receipt_data = {k: v for k, v in receipt.items() if k != 'itemArray'}
if 'itemArray' not in receipt or not receipt['itemArray']:
# If no items, return just the receipt data
return [base_receipt_data]
flattened_rows = []
for item in receipt['itemArray']:
row = base_receipt_data.copy()
# Add item data with prefix to avoid conflicts
for key, value in item.items():
row[f'item_{key}'] = value
flattened_rows.append(row)
return flattened_rows
def convert_json_to_csv(input_file: str, output_file: str) -> None:
"""
Convert JSON receipts to CSV format
"""
with open(input_file, 'r', encoding='utf-8') as f:
data = json.load(f)
all_rows = []
# Process each receipt
for receipt in data['receipts']:
flattened_items = flatten_receipt_items(receipt)
all_rows.extend(flattened_items)
if not all_rows:
print("No data to convert")
return
# Get all unique fieldnames from all rows
fieldnames = set()
for row in all_rows:
fieldnames.update(row.keys())
fieldnames = sorted(list(fieldnames))
# Write to CSV
with open(output_file, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(all_rows)
print(f"Successfully converted {len(all_rows)} rows to {output_file}")
print(f"CSV contains {len(fieldnames)} columns")
if __name__ == "__main__":
input_file = "costco2025.json"
output_file = "costco2025.csv"
if os.path.exists(input_file):
convert_json_to_csv(input_file, output_file)
else:
print(f"Input file not found: {input_file}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment