# Document JSON

Convert a PDF or an Office file to json and get its pages as data, with text blocks in reading order and tables as cells.

With `"to": "json"` in a [conversion](https://switchfile.io/docs/simple-api), you get the document as data: one JSON file with the pages of the document. Each page lists its blocks in reading order, and each table lists its cells. It holds the same text as a conversion to `md`, so you can use Markdown for reading and JSON for processing.

[GET /v1/formats](https://switchfile.io/docs/reference/get-v1-formats) lists `json` as a target for every input that supports it. The price is the same as for `md`, and PDF inputs have the same [page limits](https://switchfile.io/docs/limits#pdf-office-pages).

## Example

```json
{
  "schema": "switchfile.document",
  "version": "1.0",
  "source": { "format": "pdf" },
  "pages": [{
    "kind": "page", "number": 1, "width": 595.3, "height": 841.9,
    "blocks": [
      { "type": "heading", "level": 1, "text": "Quarterly report", "bbox": [72, 70.1, 240.5, 92.3] },
      { "type": "table", "source": "ruled", "rows": 2, "cols": 2, "header_rows": 1, "bbox": [72, 140, 523.3, 180.6],
        "cells": [
          { "row": 0, "col": 0, "text": "Region" },
          { "row": 0, "col": 1, "text": "Revenue" },
          { "row": 1, "col": 0, "text": "North" },
          { "row": 1, "col": 1, "text": "1,234.50 €", "value": { "type": "currency", "value": 1234.5, "currency": "€" } }
        ] }
    ]
  }]
}
```

The fields are also defined as a [JSON Schema](https://switchfile.io/schemas/document-1.0.json).

## Pages

What a page is depends on the input:

| Input | `kind` | Fields |
| --- | --- | --- |
| PDF | `page` | `number`, with `width` and `height` in PDF points |
| Text documents such as Word | `document` | One entry with the whole text |
| Spreadsheets | `sheet` | `number` and `name`, one table per worksheet |
| Presentations | `slide` | `number`, speaker notes in `notes` |
| Slide masters with text | `master` | `name`, listed once before the slides |

Flags on a page are present only when they apply:

| Flag | Meaning |
| --- | --- |
| `ocr` | The text comes from text recognition, as on a scanned page |
| `missing` | The page could not be read. It has no blocks |
| `text_only` | Only the words of the page could be read. Reading order and structure are not reliable |
| `hidden` | The worksheet or slide is hidden in the file |

There are no confidence scores.

## Blocks

| `type` | Fields |
| --- | --- |
| `heading` | `level` from 1 to 6 and `text` |
| `paragraph` | `text`. `rotated` marks vertical text, `in_image` text inside a picture, `quote` a quotation, `preformatted` text with its own line breaks |
| `footnote` | `text`. Only for text documents |
| `header`, `footer` | `text` of a running header or footer. The Markdown of a PDF leaves these out |
| `list` | `items`, each with `level` (0 for the top level) and `text`. Ordered items carry their `marker`, for example `3.` |
| `table` | See [Tables](https://switchfile.io/docs/document-json#tables) |
| `image` | PDF: `number`. Office files: `alt`, and only pictures with alternative text are listed. Image data is not included |

Text is plain, without Markdown. A line break inside a block is a newline. Links inside a text are listed in `links` with `text` and `url`.

Blocks from a PDF carry `bbox`, the position on the page as `[x0, y0, x1, y1]` in PDF points with the origin at the top left. Office files have no fixed positions and no `bbox`. A block with `continues` goes on at the top of the next page.

## Tables

A table has `rows` and `cols`, and its `cells` in a list. Each cell has `row` and `col`, counted from 0, and its `text` as shown. Empty cells are left out. A merged cell has `rowspan` or `colspan`, and cells never overlap.

`source` is `file` when the file stores the table as cells, as in Word or Excel. For a PDF it is `ruled` when the table is drawn with lines and `layout` when it is not.

`header_rows` is the number of header rows where it is known. A PDF table that runs over several pages stands once, on its first page, with `continues`.

## Typed values

A cell has a `value` next to its `text` when the text reads as a value without doubt, or when a spreadsheet stores one:

| `type` | `value` |
| --- | --- |
| `number` | A number, `1234.5` for `1,234.50` |
| `percent` | A fraction, `0.613` for `61.3%` |
| `currency` | A number, with `currency` as the source gives it: the sign or code printed in a PDF, the code a spreadsheet stores |
| `date` | `YYYY-MM-DD`. A spreadsheet cell with a time of day has it after a `T` |

Cells without `value` are text. In a PDF that includes cells with several lines and columns of numbers that are labels, such as article numbers. A number like `1.234` can be read in two ways. It is read the way the other numbers of the table are written, so compare `value` with `text` when such a column matters to you.

## Versions

`version` is `1.0`. Later `1.x` versions only add fields, so ignore fields you do not know. Removing or renaming a field takes a new major version with a new schema address.

## Next

- [Simple API](https://switchfile.io/docs/simple-api): Convert a file with one request to the switchfile simple API (v1). Synchronous and asynchronous requests, the conversion object, batches, ZIP files and cost estimates.
- [Formats and options](https://switchfile.io/docs/formats): Every target format with its input formats and the options it takes.
