Prompt
How do I process receipts with OCR and return JSON?
Latest observation
Oct 11, 2026
OpenAI APIWeb search: off
To process receipts with OCR and return JSON, the typical pipeline is:
-
Capture the receipt image
- Accept image/PDF upload from user.
- Preprocess if needed: deskew, denoise, increase contrast, crop.
-
Run OCR
- Use an OCR engine such as:
- Tesseract (open source)
- Google Cloud Vision
- AWS Textract
- Azure Form Recognizer
- Extract raw text and, if available, bounding boxes/coordinates.
- Use an OCR engine such as:
-
Parse the text into fields
- Identify common receipt fields:
- merchant name
- date
- total
- subtotal
- tax
- currency
- payment method
- line items
- Use rules, regex, or an LLM/post-processing model to map raw OCR text into structured data.
- Identify common receipt fields:
-
Validate and normalize
- Convert dates to ISO format
- Normalize currency symbols
- Convert amounts to numbers
- Check totals/tax consistency if possible
-
Return JSON
- Respond with a structured object.
Example JSON:
{
"merchant_name": "Starbucks",
"date": "2026-10-11",
"currency": "USD",
"subtotal": 8.75,
"tax": 0.70,
"total": 9.45,
"payment_method": "Visa",
"items": [
{
"description": "Latte",
"quantity": 1,
"unit_price": 4.25,
"line_total": 4.25
},
{
"description": "Bagel",
"quantity": 1,
"unit_price": 4.50,
"line_total": 4.50
}
],
"raw_text": "..."
}
Simple Python example with OCR + JSON extraction
import re
import json
from PIL import Image
import pytesseract
def parse_receipt_text(text):
result = {
"merchant_name": None,
"date": None,
"currency": None,
"subtotal": None,
"tax": None,
"total": None,
"raw_text": text
}
# Date example: 10/11/2026
date_match = re.search(r'(\d{1,2}[/-]\d{1,2}[/-]\d{2,4})', text)
if date_match:
result["date"] = date_match.group(1)
# Total example
total_match = re.search(r'Total\s*\$?(\d+\.\d{2})', text, re.IGNORECASE)
if total_match:
result["total"] = float(total_match.group(1))
result["currency"] = "USD"
# Merchant: first non-empty line
lines = [line.strip() for line in text.splitlines() if line.strip()]
if lines:
result["merchant_name"] = lines[0]
return result
image = Image.open("receipt.jpg")
text = pytesseract.image_to_string(image)
data = parse_receipt_text(text)
print(json.dumps(data, indent=2))
Better approach for production
For real receipts, consider:
- OCR provider with layout awareness
- field-specific parsing rules
- confidence scores
- human review for low-confidence results
- validation against known receipt patterns
If you want, I can also provide:
- a Node.js version
- a FastAPI endpoint
- an example using AWS Textract / Google Vision
- or a JSON schema for receipt data