> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pdf4llm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# to_json()

> Extract document content as structured JSON with bounding boxes, layout data, and per-block metadata.

<div id="apiIndicatorBadge">
  <div class="inner pymupdf" />
</div>

## Signature

```python theme={null}
pymupdf4llm.to_json(
    doc: str | pymupdf.Document,
    **kwargs
) -> list[dict]
```

***

## Parameters

<ParamField path="doc" type="str | pymupdf.Document" required>
  Path to the document file, or an already-opened `pymupdf.Document` instance. Supports PDF, XPS, eBooks, and — with PyMuPDF Pro — Office formats.
</ParamField>

<ParamField path="**kwargs" type="various">
  Additional parameters are shared with `to_markdown()`. See the [to\_markdown() API reference](/python/api/to_markdown) for details.
</ParamField>

<Tip>
  For other parameters, see the shared [to\_markdown() API reference](/python/api/to_markdown) which applies to all extraction functions.
</Tip>

***

## Returns

<ResponseField name="list[dict]" type="list">
  A list of page objects, one per extracted page. See [JSON Schema](/python/reference/JSON-schema) for the full field reference.

  See [Extract JSON](/python/guides/extract-JSON/index) for detailed block structure examples.
</ResponseField>

***

## Raises

| Exception           | Condition                                                                |
| ------------------- | ------------------------------------------------------------------------ |
| `FileNotFoundError` | `doc` is a path string that does not exist                               |
| `ValueError`        | An index in `pages` is out of range for the document                     |
| `ImportError`       | `ocr=True` or `force_ocr=True` but the `ocr` dependency is not installed |

***

## Examples

### Minimal

```python theme={null}
import pymupdf4llm

data = pymupdf4llm.to_json("document.pdf")
```

### Iterate over blocks

```python theme={null}
for page_num, page in enumerate(data.get("pages", [])):
    for block in page.get("boxes", []):
        for line in block.get("textlines", []):
            for span in line.get("spans", []):
                bbox = span.get("bbox", []) # bounding box for this text span
                text = span.get("text", "") # text content of the span
                flags = span.get("flags", 0) # font style flags (bitmask)
```

***

## See Also

<CardGroup cols={2}>
  <Card title="Extract JSON Guide" icon="brackets-curly" href="/python/guides/extract-JSON">
    Full walkthrough with bounding boxes, span flags, and pipeline examples.
  </Card>

  <Card title="JSON Schema" icon="file-code" href="/python/reference/JSON-schema">
    Complete field reference for every object in the JSON output.
  </Card>

  <Card title="to_markdown()" icon="markdown" href="/python/api/to_markdown">
    Markdown output for LLM ingestion and readable docs.
  </Card>

  <Card title="Tables Guide" icon="table" href="/python/guides/tables">
    Working with table blocks in the JSON output.
  </Card>
</CardGroup>
