DEV Community

Cover image for ID Card to JSON in 10 Lines of Python: OCR API + GPT-4o mini
AI Engine
AI Engine

Posted on • Originally published at ai-engine.net

ID Card to JSON in 10 Lines of Python: OCR API + GPT-4o mini

You have a photo of an ID card and you need the data inside it — name, date of birth, document number, expiration — as structured JSON. AWS Textract AnalyzeID does this, but it costs $0.025/doc and only supports US documents.

Here's how to do it for any ID card, from any country, in 10 lines of Python — for about $0.013 per document.

The Pipeline

  1. OCR API extracts raw text from the ID card image
  2. GPT-4o mini structures the raw text into label-value pairs

No regex, no templates, no per-country configuration.

The Code

import requests, json
from openai import OpenAI

def id_card_to_json(image_path):
    # Step 1: OCR
    ocr = requests.post(
        "https://ocr-wizard.p.rapidapi.com/ocr",
        headers={"x-rapidapi-key": "YOUR_KEY", "x-rapidapi-host": "ocr-wizard.p.rapidapi.com"},
        files={"image": open(image_path, "rb")},
    ).json()

    # Step 2: Structure with LLM
    result = OpenAI().chat.completions.create(
        model="gpt-4o-mini",
        response_format={"type": "json_object"},
        messages=[
            {"role": "system", "content": "Extract every label-value pair from this ID document OCR text. Return JSON: {fields: [{label, value}]}"},
            {"role": "user", "content": ocr["body"]["fullText"]},
        ],
    )
    return json.loads(result.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

Example Output

For a US driver license, the OCR returns raw text like:

ID: 012 345 678
DOB: 06-09-85
SEX: F EYES: BR HT: 5-09
CLASS D
ISSUED: 09-30-08 EXPIRES: 10-01-16
Enter fullscreen mode Exit fullscreen mode

GPT-4o mini turns it into:

{
  "document_type": "DRIVER LICENSE",
  "fields": [
    {"label": "ID", "value": "012 345 678"},
    {"label": "DOB", "value": "06-09-85"},
    {"label": "SEX", "value": "F"},
    {"label": "EYES", "value": "BR"},
    {"label": "CLASS", "value": "D"},
    {"label": "ISSUED", "value": "09-30-08"},
    {"label": "EXPIRES", "value": "10-01-16"}
  ]
}
Enter fullscreen mode Exit fullscreen mode

Tested on 4 Document Types

We tested the same code on US driver licenses (New York, Arizona), an international travel document with bilingual labels, and a US green card.

Document Fields Extracted Accuracy
US Driver License (New York) 15 100%
US Driver License (Arizona) 12 100%
International Travel Document 6 83%
US Green Card 10 100%

96% accuracy across all documents with zero custom parsing rules.

Cost: 20x Cheaper than AWS Textract

Volume AWS Textract AnalyzeID OCR API + GPT-4o mini
1K docs/mo $25.00 $13.07
10K docs/mo $250.00 $13.74
100K docs/mo $2,500.00 $100.49

Plus: AWS only supports US documents. This approach works on any country, any document type.

Use Cases

  • KYC onboarding — extract customer data from uploaded IDs
  • Age verification — parse DOB from any ID card automatically
  • Document digitization — batch process physical ID archives
  • Travel & hospitality — auto-fill check-in forms from guest IDs

👉 Read the full tutorial with complete code, all 4 test results, and feature comparison vs AWS Textract

Top comments (0)