How do you convert PDFs to Markdown and JSON for AI agents and RAG?
In three steps: read the document structurally (text, tables, headings, positions), produce Markdown for running text and JSON for fixed fields, and verify the result against the original. What matters is that every element keeps its source location – otherwise neither a human nor an agent can back up an answer later.
Markdown or JSON?
Markdown suits running text and context for RAG systems: headings, lists and tables survive as structure. JSON suits fixed fields – amounts, dates, parties, line items – with a schema that defines required fields and data types. Often you need both.
The steps
- Layout analysis and OCR: detect text, reading order, tables and images.
- Structure: mark headings, sections, lists and tables as such.
- Output: generate Markdown, extract fields against a JSON schema.
- Provenance: record page and position for every element.
- Verification: reconcile completeness and numbers against the original.
Common mistakes
Tables end up as running text in the Markdown. Headers and footers repeat in every section. Page breaks tear tables and sentences apart. And when splitting into chunks for RAG, the link to the heading is lost.
Tools
There are open-source tools that run locally and cloud services for extraction. For confidential documents, what matters most is where processing happens – and whether the result is verified to be complete. See verifying completeness.