pdfdocument-extractionocrstructured-outputai

PDF to JSON with AI: Extract Structured Data from Documents

·10 min read·AI & JSON

Why PDF-to-JSON is suddenly everywhere

A PDF is a layout format, not a data format — it knows where ink goes on a page, not that "Total: $4,200" is an invoice total. For decades, pulling structured fields out of documents meant brittle regex over OCR text or expensive template-based tools. Vision-capable LLMs changed the economics: a model like GPT-4o or Gemini can *read* a page the way a person does and emit clean JSON. By 2026 "PDF to JSON" is one of the most common AI features shipped in real products — invoice capture, contract review, resume parsing, lab reports.

Two approaches: schema-driven vs. document parsing

There are two distinct jobs, and confusing them is the most common mistake:

  • Schema-driven extraction — you define the fields you want, the model fills them. Best when you know the target shape (an invoice always has a total, a due date, line items). Output is a predictable object.
  • Document parsing — the model reconstructs the whole page into structured JSON or Markdown (headings, tables, paragraphs), preserving layout. Best when the document is unstructured or you don't know the fields in advance.

Most business workflows want the first. You supply a JSON Schema; the model returns exactly those fields:

json
{
  "invoiceNumber": "INV-2026-0417",
  "issueDate": "2026-06-30",
  "dueDate": "2026-07-30",
  "vendor": { "name": "Acme Supplies", "taxId": "GB123456789" },
  "lineItems": [
    { "description": "Aluminium sheet 2mm", "qty": 40, "unitPrice": 12.5, "amount": 500.0 }
  ],
  "subtotal": 500.0,
  "tax": 100.0,
  "total": 600.0,
  "currency": "GBP"
}

Vision models vs. OCR pipelines

Two families of tools, with a real trade-off:

  • Vision LLMs (GPT-4o, Gemini, and open models like NuExtract 3) read the rendered page directly, so they handle messy layouts, rotated scans, and tables that OCR mangles. You pass the page image plus your schema and get JSON. Downside: cost per page and occasional hallucinated values.
  • OCR + parsing services (Amazon Textract, Google Document AI, ABBYY) return structured JSON with bounding boxes and confidence scores — great when you need to know *where* on the page a value came from, for audit or human review.

A common production pattern combines them: OCR/Document AI for reliable text + coordinates, then an LLM to map that text onto your target schema.

The rule that keeps you out of trouble: validate everything

The model's JSON *looks* authoritative, which is exactly the danger — a hallucinated total is worse than a missing one. Never write extracted data straight to a database. Validate first:

  1. Enforce a JSON Schema on every extraction so a missing total or a string where you expect a number is rejected before it propagates.
  2. Add arithmetic checks the schema can't express — do lineItems sum to subtotal? Does subtotal + tax == total? Cross-field math catches a huge share of extraction errors.
  3. When a document extracts oddly, format the raw output and diff it against a known-good example from the same template to spot the drifted field.
  4. Route low-confidence extractions to human review rather than trusting them silently.

To generate typed models for the extracted shape, run your schema through JSON to Python, TypeScript, or Go. For getting reliable JSON out of the model in the first place, see Get JSON From LLMs and Fix Broken JSON From LLMs.

Frequently asked questions

Use a vision LLM when layouts are messy or variable and you want fields mapped straight to a schema. Use an OCR/Document AI service when you need bounding boxes, confidence scores, and an audit trail of where each value came from. Many pipelines combine both: OCR for reliable text, an LLM to structure it.

Constrain it with a strict JSON Schema, keep the prompt focused on "extract only what's present, use null if absent," and validate the output with cross-field checks (totals that must sum, dates that must be ordered). Send low-confidence results to human review.

Schema-driven extraction fills a target shape you define — ideal when you know the fields. Document parsing reconstructs the whole page into structured JSON/Markdown — ideal for unknown or unstructured documents where you want the layout preserved.

Yes. Vision models return table rows as arrays of objects, and OCR services like Textract have dedicated table extraction. Validate row counts and column alignment, since tables are where extraction errors cluster.

Try JSON Schema Validator

Validate extracted document JSON against a schema before you store it.