{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs2json.com/schema/1.0/docs2json.schema.json",
  "title": "docs2json",
  "description": "One document converted to structured JSON. Three parts: `schema` names the format and its version, `metadata` describes the file and where each value came from, and `content` holds the pages and every element on them in reading order. Every field has a fixed path and meaning, so it can be mapped into another schema without walking a tree or resolving a pointer.",
  "type": "object",
  "required": ["schema", "metadata", "content"],
  "additionalProperties": false,
  "properties": {
    "schema": { "$ref": "#/$defs/schemaInfo" },
    "metadata": { "$ref": "#/$defs/metadata" },
    "content": { "$ref": "#/$defs/content" }
  },
  "$defs": {
    "schemaInfo": {
      "description": "Which format this file is, and which version of it.",
      "type": "object",
      "required": ["name", "version"],
      "additionalProperties": false,
      "properties": {
        "name": {
          "description": "Always `docs2json`.",
          "const": "docs2json"
        },
        "version": {
          "description": "Format version, `major.minor`. The minor number goes up when a field is added and existing mappings keep working. The major number goes up when a change breaks a mapping.",
          "type": "string",
          "pattern": "^[0-9]+\\.[0-9]+$",
          "examples": ["1.0"]
        }
      }
    },

    "metadata": {
      "description": "Facts about the document, grouped by where each value comes from.",
      "type": "object",
      "required": ["file", "stats", "processing"],
      "additionalProperties": false,
      "properties": {
        "file": { "$ref": "#/$defs/file" },
        "properties": { "$ref": "#/$defs/properties" },
        "stats": { "$ref": "#/$defs/stats" },
        "processing": { "$ref": "#/$defs/processing" },
        "extracted": {
          "description": "Facts a language model read from the document's first and last pages, named in the document's own vocabulary (an invoice gives `invoice_number`, a contract gives `effective_date`). Optional paid enrichment: absent when it was not ordered.",
          "type": "array",
          "items": { "$ref": "#/$defs/extractedFact" }
        }
      }
    },

    "file": {
      "description": "The file as it was received.",
      "type": "object",
      "required": ["filename", "media_type", "format", "size_bytes", "sha256", "page_count"],
      "additionalProperties": false,
      "properties": {
        "filename": {
          "description": "The file's name as it was received.",
          "type": "string",
          "examples": ["q2fy27pr.pdf"]
        },
        "media_type": {
          "description": "IANA media type of the file as received.",
          "type": "string",
          "examples": ["application/pdf", "image/png", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"]
        },
        "format": {
          "description": "The file's format family. `image` covers PNG, JPEG, TIFF, BMP and WebP.",
          "enum": ["pdf", "image", "docx", "pptx", "xlsx", "csv", "txt"]
        },
        "size_bytes": {
          "description": "Size of the file as received, in bytes.",
          "type": "integer",
          "minimum": 0
        },
        "sha256": {
          "description": "SHA-256 of the file as received, lowercase hex. Two files with the same value are the same bytes.",
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        "page_count": {
          "description": "Number of pages. For DOCX, XLSX, CSV and TXT this is the page count of the file laid out as pages, since those formats have no fixed pages of their own. For PPTX it is the slide count, and for an image it is 1.",
          "type": "integer",
          "minimum": 0
        }
      }
    },

    "properties": {
      "description": "Properties the file itself declares, such as a PDF's document information or a Word file's core properties. Output exactly as the file declares them: they are often wrong or left at a template's default (\"Microsoft Word - Document1\"). A field is absent when the file declares nothing for it. CSV and TXT files declare no properties.",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "title": { "$ref": "#/$defs/stringProperty", "description": "Declared title." },
        "authors": {
          "description": "Declared authors, meant to be people. Files often hold a company or a username here instead. A file declaring one author string gives a one-item list; names are never split.",
          "type": "object",
          "required": ["value", "source"],
          "additionalProperties": false,
          "properties": {
            "value": { "type": "array", "items": { "type": "string" }, "minItems": 1 },
            "source": { "$ref": "#/$defs/propertySource" }
          }
        },
        "subject": { "$ref": "#/$defs/stringProperty", "description": "Declared subject." },
        "keywords": { "$ref": "#/$defs/stringProperty", "description": "Declared keywords, as one string exactly as the file holds them." },
        "description": { "$ref": "#/$defs/stringProperty", "description": "Declared description or abstract." },
        "created": { "$ref": "#/$defs/dateProperty", "description": "Declared creation date." },
        "modified": { "$ref": "#/$defs/dateProperty", "description": "Declared last-modified date." },
        "language": {
          "description": "Declared language, as an ISO 639-1 code. A file declaring `en-US` gives `en`.",
          "type": "object",
          "required": ["value", "source"],
          "additionalProperties": false,
          "properties": {
            "value": { "$ref": "#/$defs/languageCode" },
            "source": { "$ref": "#/$defs/propertySource" }
          }
        },
        "company": { "$ref": "#/$defs/stringProperty", "description": "Declared company. Office files only." },
        "application": { "$ref": "#/$defs/stringProperty", "description": "The application that created or produced the file." },
        "copyright": { "$ref": "#/$defs/stringProperty", "description": "Declared copyright or rights statement." }
      }
    },

    "stringProperty": {
      "type": "object",
      "required": ["value", "source"],
      "additionalProperties": false,
      "properties": {
        "value": { "type": "string" },
        "source": { "$ref": "#/$defs/propertySource" }
      }
    },

    "dateProperty": {
      "type": "object",
      "required": ["value", "source"],
      "additionalProperties": false,
      "properties": {
        "value": {
          "description": "ISO 8601. A date declared with a timezone is converted to UTC and ends in `Z`. A date declared without one is output without an offset, because the file does not say which timezone it meant.",
          "type": "string",
          "examples": ["2026-08-26T20:21:04Z", "2026-08-26T13:21:04"]
        },
        "source": { "$ref": "#/$defs/propertySource" }
      }
    },

    "propertySource": {
      "description": "Where in the file the value was read.",
      "type": "string",
      "examples": ["PDF Info /Title", "XMP dc:creator", "core.xml dc:title", "app.xml Company", "EXIF Artist"]
    },

    "stats": {
      "description": "Counts and facts computed from the converted content.",
      "type": "object",
      "required": ["word_count", "table_count", "figure_count"],
      "additionalProperties": false,
      "properties": {
        "word_count": {
          "description": "Words in the text of every element, page headers and footers included. A table counts the words in its cells, not in its Markdown rendering.",
          "type": "integer",
          "minimum": 0
        },
        "table_count": {
          "description": "Number of `table` elements.",
          "type": "integer",
          "minimum": 0
        },
        "figure_count": {
          "description": "Number of `figure` elements.",
          "type": "integer",
          "minimum": 0
        },
        "language": {
          "description": "The main language of the text, detected from the content rather than declared by the file, as an ISO 639-1 code. Absent when it cannot be detected.",
          "$ref": "#/$defs/languageCode"
        }
      }
    },

    "processing": {
      "description": "How and when this file was produced.",
      "type": "object",
      "required": ["engine_version", "converted_at"],
      "additionalProperties": false,
      "properties": {
        "engine_version": {
          "description": "Version of the conversion engine. It changes whenever conversion output can change. Element ids are stable only for the same file through the same `engine_version`.",
          "type": "string"
        },
        "converted_at": {
          "description": "When the conversion ran, ISO 8601 in UTC.",
          "type": "string",
          "format": "date-time",
          "examples": ["2026-11-13T10:34:12Z"]
        }
      }
    },

    "extractedFact": {
      "description": "One fact a language model read from the document.",
      "type": "object",
      "required": ["name", "value", "source_element_id", "generated_by"],
      "additionalProperties": false,
      "properties": {
        "name": {
          "description": "The fact's name, in the document's own vocabulary, snake_case.",
          "type": "string",
          "examples": ["invoice_number", "effective_date"]
        },
        "value": {
          "description": "The value as the model read it.",
          "type": ["string", "null"]
        },
        "source_element_id": {
          "description": "`id` of the element the value was read from, or null when the model could not name one.",
          "oneOf": [{ "$ref": "#/$defs/elementId" }, { "type": "null" }]
        },
        "generated_by": {
          "description": "Always `llm`: the value was produced by a language model, not copied from the file.",
          "const": "llm"
        }
      }
    },

    "content": {
      "description": "The document's pages, and every element on them in reading order.",
      "type": "object",
      "required": ["pages", "elements"],
      "additionalProperties": false,
      "properties": {
        "pages": {
          "description": "Every page that converted, in order. A partial conversion lists fewer pages than `metadata.file.page_count`.",
          "type": "array",
          "items": { "$ref": "#/$defs/page" }
        },
        "elements": {
          "description": "Every element in reading order. The array order is the reading order; do not sort by `id`.",
          "type": "array",
          "items": { "$ref": "#/$defs/element" }
        }
      }
    },

    "page": {
      "type": "object",
      "required": ["number", "width", "height"],
      "additionalProperties": false,
      "properties": {
        "number": { "description": "Page number, from 1.", "type": "integer", "minimum": 1 },
        "width": { "description": "Page width in points (1/72 inch).", "type": "number", "exclusiveMinimum": 0 },
        "height": { "description": "Page height in points (1/72 inch).", "type": "number", "exclusiveMinimum": 0 }
      }
    },

    "elementId": {
      "description": "Element id, `p{page}-e{n}`: the n-th element in reading order on the page where the element starts. Unique within the file. Stable for the same file through the same `engine_version`, and nothing more: do not parse it or sort by it.",
      "type": "string",
      "pattern": "^p[0-9]+-e[1-9][0-9]*$",
      "examples": ["p3-e12"]
    },

    "box": {
      "description": "A rectangle on a page in points (1/72 inch), origin at the top-left corner of the page, `y` growing downward. Every box in the file uses this one system.",
      "type": "object",
      "required": ["x0", "y0", "x1", "y1"],
      "additionalProperties": false,
      "properties": {
        "x0": { "description": "Left edge.", "type": "number" },
        "y0": { "description": "Top edge.", "type": "number" },
        "x1": { "description": "Right edge.", "type": "number" },
        "y1": { "description": "Bottom edge.", "type": "number" }
      }
    },

    "pageBox": {
      "description": "One region of an element, on one page. Coordinates as in a table cell's `box`: points, top-left origin, `y` growing downward.",
      "type": "object",
      "required": ["page", "x0", "y0", "x1", "y1"],
      "additionalProperties": false,
      "properties": {
        "page": { "description": "Page number, from 1.", "type": "integer", "minimum": 1 },
        "x0": { "description": "Left edge.", "type": "number" },
        "y0": { "description": "Top edge.", "type": "number" },
        "x1": { "description": "Right edge.", "type": "number" },
        "y1": { "description": "Bottom edge.", "type": "number" }
      }
    },

    "element": {
      "description": "One block of the document: a heading, a paragraph, a table, a figure and so on. Each element carries everything about itself; no field points into another structure except by element `id`.",
      "type": "object",
      "required": ["id", "type", "role", "pages", "boxes", "headings"],
      "additionalProperties": false,
      "properties": {
        "id": { "$ref": "#/$defs/elementId" },
        "type": {
          "description": "What the element is. `title` is the document title; `heading` is a section heading; `paragraph` is running text, and also page headers and footers (see `role`); `list` groups `list_item` elements; `table` holds cells; `figure` is a picture or chart; `caption` and `footnote` belong to a table, figure or code block; `formula` is a mathematical expression; `code` is a code block; `checkbox` is a form checkbox.",
          "enum": ["title", "heading", "paragraph", "list", "list_item", "table", "figure", "caption", "footnote", "formula", "code", "checkbox"]
        },
        "role": {
          "description": "`body` for the document's content. `header` and `footer` for text repeated at the top or bottom of pages, such as running titles and page numbers, which most consumers filter out.",
          "enum": ["body", "header", "footer"]
        },
        "text": {
          "description": "The element's text. On a `table`, one string for consumers who want the table as text: its caption(s), then its cells as a Markdown table, then its footnote(s), separated by blank lines; the cells are in `table`. The caption and footnotes are also elements of their own, so a consumer joining every element's `text` sees them twice. Absent on `list` (its text is in its items) and on `figure`.",
          "type": "string"
        },
        "raw": {
          "description": "The text exactly as the source held it, present only when it differs from `text` (for example a list item whose source text includes its bullet).",
          "type": "string"
        },
        "text_source": {
          "description": "Where `text` came from. `embedded`: the file contains the text itself. `ocr`: the text was recognised from pixels, so it may hold recognition errors. `mixed`: some of each. A scanned PDF that already carries a hidden text layer reads as `embedded`. Present whenever `text` is.",
          "enum": ["embedded", "ocr", "mixed"]
        },
        "pages": {
          "description": "The pages this element is on, from 1, ascending. Empty only for an element with no position on any page.",
          "type": "array",
          "items": { "type": "integer", "minimum": 1 },
          "uniqueItems": true
        },
        "boxes": {
          "description": "Where the element sits, one box per region. Most elements have one; an element continuing across a page or column break has one per part. A `list` has the box enclosing its items, per page.",
          "type": "array",
          "items": { "$ref": "#/$defs/pageBox" }
        },
        "headings": {
          "description": "The text of the document's own headings above this element, outermost first. Empty before the first heading. When a document marks every heading at the same level, this holds only the nearest one.",
          "type": "array",
          "items": { "type": "string" }
        },
        "parent": {
          "description": "`id` of the `list` this item belongs to. On `list_item` only.",
          "$ref": "#/$defs/elementId"
        },
        "level": {
          "description": "Heading depth, from 1 for the outermost. On `heading` only.",
          "type": "integer",
          "minimum": 1
        },
        "ordered": {
          "description": "Whether the list is numbered. On `list` only.",
          "type": "boolean"
        },
        "marker": {
          "description": "The item's own numbering or bullet as printed, such as `(a)`, `1.` or `iii.`. On `list_item` only, absent when the item has none.",
          "type": "string",
          "minLength": 1
        },
        "checked": {
          "description": "Whether the checkbox is ticked. On `checkbox` only.",
          "type": "boolean"
        },
        "code_language": {
          "description": "The programming language of the code block, when known. On `code` only.",
          "type": "string",
          "examples": ["python", "sql"]
        },
        "table": { "$ref": "#/$defs/table" },
        "image": { "$ref": "#/$defs/image" },
        "captions": {
          "description": "`id`s of this element's captions. On `table`, `figure` and `code` only.",
          "type": "array",
          "items": { "$ref": "#/$defs/elementId" },
          "minItems": 1
        },
        "footnotes": {
          "description": "`id`s of this element's footnotes. On `table`, `figure` and `code` only.",
          "type": "array",
          "items": { "$ref": "#/$defs/elementId" },
          "minItems": 1
        }
      },
      "dependentRequired": {
        "text": ["text_source"],
        "text_source": ["text"],
        "raw": ["text"]
      },
      "allOf": [
        { "$ref": "#/$defs/onlyOn/text" },
        { "$ref": "#/$defs/onlyOn/parent" },
        { "$ref": "#/$defs/onlyOn/level" },
        { "$ref": "#/$defs/onlyOn/ordered" },
        { "$ref": "#/$defs/onlyOn/marker" },
        { "$ref": "#/$defs/onlyOn/checked" },
        { "$ref": "#/$defs/onlyOn/code_language" },
        { "$ref": "#/$defs/onlyOn/table" },
        { "$ref": "#/$defs/onlyOn/image" },
        { "$ref": "#/$defs/onlyOn/captions" },
        { "$ref": "#/$defs/onlyOn/footnotes" }
      ]
    },

    "onlyOn": {
      "$comment": "Which element types may carry each type-specific field, and which must.",
      "text": {
        "if": { "properties": { "type": { "enum": ["list", "figure"] } } },
        "then": { "not": { "required": ["text"] } },
        "else": {
          "if": { "properties": { "type": { "const": "table" } } },
          "then": { "required": ["text"] }
        }
      },
      "parent": {
        "if": { "properties": { "type": { "const": "list_item" } } },
        "then": { "required": ["parent"] },
        "else": { "not": { "required": ["parent"] } }
      },
      "level": {
        "if": { "properties": { "type": { "const": "heading" } } },
        "then": { "required": ["level"] },
        "else": { "not": { "required": ["level"] } }
      },
      "ordered": {
        "if": { "properties": { "type": { "const": "list" } } },
        "then": { "required": ["ordered"] },
        "else": { "not": { "required": ["ordered"] } }
      },
      "marker": {
        "if": { "properties": { "type": { "const": "list_item" } } },
        "else": { "not": { "required": ["marker"] } }
      },
      "checked": {
        "if": { "properties": { "type": { "const": "checkbox" } } },
        "then": { "required": ["checked"] },
        "else": { "not": { "required": ["checked"] } }
      },
      "code_language": {
        "if": { "properties": { "type": { "const": "code" } } },
        "else": { "not": { "required": ["code_language"] } }
      },
      "table": {
        "if": { "properties": { "type": { "const": "table" } } },
        "then": { "required": ["table"] },
        "else": { "not": { "required": ["table"] } }
      },
      "image": {
        "if": { "properties": { "type": { "const": "figure" } } },
        "else": { "not": { "required": ["image"] } }
      },
      "captions": {
        "if": { "properties": { "type": { "enum": ["table", "figure", "code"] } } },
        "else": { "not": { "required": ["captions"] } }
      },
      "footnotes": {
        "if": { "properties": { "type": { "enum": ["table", "figure", "code"] } } },
        "else": { "not": { "required": ["footnotes"] } }
      }
    },

    "table": {
      "description": "The table's cells. On `table` only. Each cell appears once; a cell spanning several rows or columns appears at its top-left position.",
      "type": "object",
      "required": ["rows", "cols", "cells"],
      "additionalProperties": false,
      "properties": {
        "rows": { "description": "Number of rows.", "type": "integer", "minimum": 0 },
        "cols": { "description": "Number of columns.", "type": "integer", "minimum": 0 },
        "cells": {
          "type": "array",
          "items": { "$ref": "#/$defs/cell" }
        }
      }
    },

    "cell": {
      "type": "object",
      "required": ["row", "col", "row_span", "col_span", "text"],
      "additionalProperties": false,
      "properties": {
        "row": { "description": "Row of the cell's top-left position, from 0.", "type": "integer", "minimum": 0 },
        "col": { "description": "Column of the cell's top-left position, from 0.", "type": "integer", "minimum": 0 },
        "row_span": { "description": "Rows the cell covers.", "type": "integer", "minimum": 1 },
        "col_span": { "description": "Columns the cell covers.", "type": "integer", "minimum": 1 },
        "header": {
          "description": "`column`: the cell heads its column. `row`: it heads its row. `section`: it is a row that labels the rows below it. Absent for a data cell. Header detection is not perfect: a header the engine missed reads as a data cell.",
          "enum": ["column", "row", "section"]
        },
        "text": { "description": "The cell's text. Empty for an empty cell.", "type": "string" },
        "box": { "$ref": "#/$defs/box" }
      }
    },

    "image": {
      "description": "The figure's pixels, delivered as a file beside this JSON. On `figure` only; absent when no image could be cut from the page.",
      "type": "object",
      "required": ["file", "media_type", "width", "height"],
      "additionalProperties": false,
      "properties": {
        "file": {
          "description": "Path of the image file, relative to this JSON file.",
          "type": "string",
          "examples": ["figures/p1-e3.png"]
        },
        "media_type": {
          "description": "IANA media type of the image file.",
          "type": "string",
          "examples": ["image/png"]
        },
        "width": { "description": "Width in pixels.", "type": "integer", "minimum": 1 },
        "height": { "description": "Height in pixels.", "type": "integer", "minimum": 1 }
      }
    },

    "languageCode": {
      "description": "ISO 639-1 language code, two lowercase letters.",
      "type": "string",
      "pattern": "^[a-z]{2}$",
      "examples": ["en", "de"]
    }
  }
}
