PDF to JSON in Python: three meanings, and how to do each
Updated · Tested with Python 3.13, pypdf 7.2.0, PyMuPDF 2.2
In short
PDF to JSON can mean the text of each page, the whole layout with positions, or named fields such as a company number. The first is two lines with any PDF library and the second is one call in PyMuPDF. The third needs rules you write yourself or a service, because only you know which values you want.
"PDF to JSON" is three different jobs, and the right code depends on which one you mean. The examples use company-extract.pdf.
| You want | The JSON holds | Use |
|---|---|---|
| The text, in a form a program can pass around | One string per page | Any PDF library |
| The layout | Every block, line and word with its position and font | PyMuPDF or pdfplumber |
| The data | Named values such as company_number | Rules you write, or a service |
1. The text of each page as JSON
import json
import pymupdf
with pymupdf.open("company-extract.pdf") as doc:
pages = [{"page": number, "text": page.get_text()} for number, page in enumerate(doc, start=1)]
print(json.dumps(pages)[:120])[{"page": 1, "text": "COMPANY EXTRACT\nCompany name\nHarbor Lane Logistics Ltd\nCompany number\n51-482913-6\nStatus\nActThis is all most search indexes and language models need. Keep the page number: it lets you point back to where an answer came from.
2. The layout as JSON
PyMuPDF can write the whole structure of a page as JSON: blocks, the lines in them, the spans in each line, and for every span its text, font, size, colour and position.
import json
import pymupdf
with pymupdf.open("company-extract.pdf") as doc:
page = json.loads(doc[0].get_text("json"))
print(list(page.keys()))
span = page["blocks"][1]["lines"][0]["spans"][0]
print(span["text"], span["font"], round(span["size"], 1), [round(v, 1) for v in span["bbox"]])['width', 'height', 'blocks']
Company name Noto-Sans 11.0 [68.4, 129.0, 149.9, 144.0]For this one-page sample that JSON is about 16,000 characters. Layout JSON grows fast, so ask for it only when you will use the positions; text with coordinates shows what to do with them.
3. Named fields as JSON
This is what people usually mean: not the page, but the facts on it. No library can do it for you, because only you know which values you want. The simplest method is a pattern for each one:
import json
import re
from pypdf import PdfReader
text = PdfReader("company-extract.pdf").pages[0].extract_text()
fields = {
"company_name": re.search(r"Company name (.+)", text).group(1),
"company_number": re.search(r"Company number ([\d-]+)", text).group(1),
"status": re.search(r"Status (\w+)", text).group(1),
"share_capital": float(re.search(r"Share capital ([\d,.]+)", text).group(1).replace(",", "")),
}
print(json.dumps(fields, indent=2)){
"company_name": "Harbor Lane Logistics Ltd",
"company_number": "51-482913-6",
"status": "Active",
"share_capital": 1250.5
}Note the last line of the code: the number arrives as the text 1,250.50, and it is your job to turn it into 1250.5.
Patterns work well while every document looks like the one you wrote them for. They break quietly when a label is reworded, a value wraps onto a second line, or the file is a scan with no text at all. re.search then returns None, and the line fails; check for that in real code.
Which library for the text?
pypdf is used in the third example for a reason. It returns each label and its value on one line, which is what the patterns rely on. PyMuPDF returns them on separate lines unless you pass sort=True. The comparison of the Python libraries lists the differences.
Named fields without writing patterns
100sands does the third job as a service, for PDFs and HTML files that contain text. You upload one to five samples of a kind of document, AI suggests the fields and their values, and you check and correct them. It then sets up the extraction, which goes live only when it returns every value you checked on every sample. After that, each document of that kind comes back as JSON from one call, with no AI involved:
curl https://api.100sands.com/v1/document-types/42/extract \
-H "Authorization: Bearer $SANDS_API_KEY" \
-F [email protected]The response has this shape:
{
"id": "ext_...",
"documentType": { "id": 42, "name": "Company registry extract" },
"version": 2,
"fileName": "company-extract.pdf",
"pages": 1,
"createdAt": "2026-12-04T09:30:00Z",
"fields": {
"companyName": "Harbor Lane Logistics Ltd",
"companyNumber": "51-482913-6",
"status": "Active",
"registered": "2019-03-14",
"shareCapital": 1250.5
}
}Each value is taken character for character from the file's text; number fields then come back as JSON numbers, and dates as YYYY-MM-DD when their format is known. A field that is not in the document is null, never a guess. The API docs have the details. The four ways to extract data from a PDF compares this with patterns, positions and language models.