PDF to JSON: How to Extract Structured Data via API

Converting a PDF to JSON is a different problem than converting it to CSV, real documents mix flat tables with nested key-value data. Here's how to extract PDFs as structured JSON through an API, and how to avoid the integration pitfalls that trip up automated pipelines.

Most guides on getting data out of a PDF focus on spreadsheets, converting to CSV or Excel for someone to open and read. But if you're building software rather than doing manual bookkeeping, what you actually want is usually JSON: structured, nested, machine-readable data you can feed directly into your own application, database, or pipeline without a spreadsheet in the middle at all. This guide covers what "PDF to JSON" actually means in practice, why it's a harder problem than it sounds, and how to do it through an API.

Why PDF to JSON Is Not the Same Problem as PDF to CSV

A CSV file is fundamentally flat, one table, rows and columns, nothing nested. A lot of real-world documents don't fit that shape at all. An invoice has line items (a table) alongside header fields like vendor name and invoice number (key-value pairs) and possibly a nested tax breakdown. A multi-page report might have several distinct tables that shouldn't be merged into one. JSON is a better fit for this because it naturally represents both flat tables and nested key-value structures in the same document, which is exactly what most real PDFs actually contain.

This means a genuinely useful PDF-to-JSON tool needs to do two things well: recognize which parts of a document are tabular data versus which parts are metadata or key-value fields, and produce a JSON structure that reflects that distinction instead of flattening everything into one shape regardless of what it actually represents.

What Good Structured Output Looks Like

For a table-heavy document, useful JSON output is an array of objects, one per row, with consistent keys across every object, the equivalent of a CSV, but as JSON instead of a flat file. For a document that's mostly key-value data, like an invoice header or a form, useful output is a flat object with clearly named keys rather than a generic array of ambiguous field-value pairs. And for documents that have both, like an invoice with a line-item table under a header of key-value fields, useful output nests the table inside the surrounding metadata rather than forcing everything into one flat structure that loses that relationship.

The keys themselves matter too. Field names like "field_1" and "field_2" are technically valid JSON but not actually useful, you want keys that reflect what the data actually is, like "invoice_number" and "line_items", so the output is usable without a separate mapping step to figure out what each field means.

Extracting PDF Data as JSON

DataFromFile's extraction API accepts a file and returns structured JSON directly, you're not scraping a webpage or parsing a spreadsheet export, you're getting a JSON response back from a single request. A basic request looks like this:

POST /api/v1/extract
{
  "fileName": "invoice.pdf",
  "mimeType": "application/pdf",
  "fileData": "<base64-encoded file>",
  "mode": "auto"
}

The response is `{ success: true, data: { summary, rowCount, columnNames, records, keyValues } }`: `records` holds the table rows, `columnNames` lists the columns, and `keyValues` holds header-style fields such as an invoice number. The `mode` parameter controls how the document gets interpreted: `auto` lets the model decide whether the document is primarily tabular or primarily key-value based on its actual content, `tables` forces a strict row-and-column structure, and `key_values` extracts header-style metadata fields. For documents with a known, fixed structure you want enforced consistently across many files, a `schema` mode is also available, where you pass the shape you expect in `targetSchema` and the extraction is steered to it. If you'd rather explore the same underlying engine through a browser first, the PDF data extraction tool uses the identical extraction pipeline with a UI on top of it.

Handling Tables vs. Key-Value Data in the Same Document

The most common real-world case is a document that's genuinely both, an invoice with header fields (date, vendor, invoice number) and a line-item table underneath. Rather than choosing one shape and losing information, `auto` mode returns both: the header-style fields in `keyValues` and the table rows in `records`. That means your application code can read the invoice number from `keyValues` and iterate over `records` separately, instead of trying to reconstruct that separation from a flat table where every row awkwardly repeats the same header fields.

Common Integration Pitfalls

A few issues come up repeatedly when integrating PDF-to-JSON extraction into an application rather than using it as a one-off tool.

Inconsistent keys across documents is the most disruptive one for downstream code, if one invoice returns "total" and another returns "invoice_total", any code consuming the output has to handle both, which defeats much of the point of structured extraction. Using `schema` mode with an explicit `targetSchema` helps for documents where you control or can predict the format, since the extraction is steered toward the same field names regardless of how the source document happened to label things. Check a few real responses before relying on it.

Numbers returned as strings instead of numeric types is another common friction point, a value like "1,204.50" needs to be parsed before it can be used in arithmetic, and depending on locale, that comma might be a thousands separator or a decimal point. It's worth normalizing numeric fields explicitly in your own code rather than assuming a particular format, especially if you're processing documents from multiple regions.

Multi-page documents can also produce a table that got split at a page boundary, check whether a table you expect to be one continuous set of rows has actually been returned as one array rather than accidentally split, particularly for very long tables spanning many pages.

When to Use JSON vs. CSV

If you're building an application or automated pipeline, JSON is generally the right choice because it preserves structure a flat file can't represent. If the end goal is a spreadsheet a person will open and review manually, CSV is simpler and more directly usable. And if you're loading extracted data into a database, JSON maps cleanly onto table columns, so a short loader script can turn it into `INSERT` statements. We cover this tradeoff in more depth in CSV vs. JSON vs. Parquet: choosing the right output for automated data pipelines.

Frequently Asked Questions

Does the API handle scanned PDFs and images, not just digital PDFs?

Yes, the same extraction pipeline handles scanned documents and images through multimodal vision processing, not just text-layer PDFs. Output format (JSON, CSV, Excel, and others) is independent of whether the source was a native PDF, a scan, or a photo.

Can I enforce a specific JSON schema across many different documents?

Yes. Use `schema` mode and pass the structure you want in `targetSchema`, and extraction is steered to that shape rather than inferring a potentially different structure for each file.

What's the maximum file size?

A request to this endpoint carries the file inline as base64, which the hosting provider caps at about 3 MB per request, whatever your plan. Larger files go through the web app's own upload flow, where the limit follows your plan: 3 MB on Free, 10 MB on Pro, 25 MB on Business and 100 MB on Enterprise.

Is there a way to test this before integrating it into code?

Yes, the PDF data extraction tool runs the exact same extraction engine through a browser interface, so you can upload a real document and inspect the JSON output before writing any integration code against it.

If you're building something that needs structured document data as part of an application rather than a one-time export, the API reference has the full request and response format, and you can test extraction directly against a real file using the PDF data extraction tool first.

Keep reading