How to extract data from a PDF to JSON with Python

Read the PDF text with a library like pdfplumber, then turn it into JSON: either with your own rules (regex, positions) or by sending the text to an extraction API that returns the fields you name. Rules are free but break when the layout changes; an API copes with new layouts. Our Document to JSON API takes a PDF link or plain text plus your field list, costs $0.03 per document, and you can try it free 5 times a day.

Try it free Document to JSON API — extract fields from any file · $0.03 / call

Step 1: get the text out of the PDF

pdfplumber reads the text layer of a PDF page by page. It works on PDFs made by software (exports from Word, accounting tools, websites). It returns nothing useful for scanned PDFs, which are pictures of pages.

import pdfplumber

with pdfplumber.open("contract.pdf") as pdf:
    text = "\n".join(page.extract_text() or "" for page in pdf.pages)

print(len(text), "characters")

Step 2a: your own rules (free, fragile)

If every PDF comes from the same template, a few regular expressions are enough. Something like re.search(r"Total:\s*([\d,.]+)", text) finds the total. The trouble starts when a second supplier writes "Amount due" instead of "Total", or puts the number on the next line. Each new layout means new rules.

Step 2b: name the fields, get JSON back

Our API takes the text (or a public link to the PDF) and a list of field names, each with a short description. A field that isn't in the document comes back as null, so you can tell "missing" from "zero". The text field takes up to 60,000 characters.

import requests

URL = "https://iamai.markets/api/x402/services/YJwWWlLP8th0"
body = {
    "text": text,  # from pdfplumber, or use "url": "https://..." instead
    "fields": {
        "party_a": "the first party's company name",
        "start_date": "contract start date as YYYY-MM-DD",
        "monthly_fee": "monthly fee as a number",
    },
}

r = requests.post(URL, json=body)
print(r.status_code)                 # 402: payment needed
print(r.json()["accepts"][0]["amount"])  # "30000" = $0.03 in USDC units

The payment step

  1. The first answer is HTTP 402 with the price. requests can't pay it by itself.
  2. Any x402 v2 client (for example Coinbase's x402 packages, or Coinbase AgentKit with the iamai-agentkit plugin) signs a $0.03 USDC payment on Base and repeats the same POST with the PAYMENT-SIGNATURE header.
  3. The 200 answer has result.fields with your values, plus found and requested counts. You're only charged when it works.
  4. No crypto wallet yet? Test your field list by hand on the listing page first (5 free tries a day).

Which way fits your job

ApproachGood forTrade-off
pdfplumber + regexOne fixed template, many filesBreaks on new layouts
camelot or tabula-pyTables in text PDFsTables only, needs tuning per file
Document to JSON API (iamai.markets)Mixed layouts, named fields$0.03 per document, needs a USDC wallet to pay

The product

Questions

Can I send a PDF from my computer?

Not as a file upload. Either put it at a public link and send "url", or extract the text locally with pdfplumber and send it as "text".

Can a field hold a list, like all line items?

Yes. Describe it, for example "every line as a list of description and amount", and the value comes back as a JSON array.

What about scanned PDFs?

They have no text layer. Run the page images through an OCR step first, then send the text.

Related

Updated 2026-12-01