# Complex HTML Table Extractor API: Rowspan, Colspan & Multi-Level Headers to Typed JSON

> Target Query: `html table rowspan colspan to json api` | Category: `Extraction Modality` | HTML Guide: https://scrape.fastmesh.dev/seo_geo/html-table-rowspan-colspan-to-json-api.html

## Direct Answer
Naive BeautifulSoup or regex table scrapers shift columns horizontally the moment a table contains `rowspan`, `colspan`, or two-tier `<thead>` group headers, silently corrupting pricing schedules and regulatory filings. Scrape-Anything runs a deterministic `HTMLParser` 2D cell-occupancy grid that expands every `rowspan` and `colspan`, flattens multi-level headers into unambiguous column keys, separates `<tfoot>` summary rows, normalizes numbers and currencies, and validates every row against your JSON Schema.

## Engineering Mechanics & Deterministic Guardrails
- True 2D grid occupancy algorithm places every cell at its exact `(row, col)` coordinate across overlapping `rowspan` and `colspan` attributes.
- Multi-level `<thead>` flattening merges parent/child header bands (e.g., `Revenue > North America`) into deterministic keys.
- Preserves `<tfoot>` summary rows separately so grand totals do not pollute line-item arrays.
- Built-in numeric and currency normalizer parses `$1,240.50`, `€890.00`, `(42.5)`, and `14.2%` cleanly while retaining raw display strings.

## HTML Table Edge Cases Handled by `html_tables.py`

| HTML Table Pattern | Why Naive Scrapers Break | Scrape-Anything Grid Resolution | Schema Safety |
| :--- | :--- | :--- | :--- |
| `colspan="2"` header groups | Header count != data cell count; columns drift | Expands parent header across spanned columns and joins child header | Stable column names across rows |
| `rowspan="3"` category labels | Rows 2 and 3 shift left by 1 column | Occupies `(r+1, c)` and `(r+2, c)` and carries group value cleanly | Zero column misalignment |
| `<tfoot>` totals row | Mixed into line items, double-counting sums | Separated into explicit `footer_rows` metadata | Clean aggregate math |
| Formatted currencies (`$1,499.00`) | Stored as dirty strings breaking numeric schemas | Numeric/currency parser extracts typed float & currency code | Passes `type: number` validation |
| `rel="next"` table pagination | Truncated after page 1 | Extracts `pagination.next_url` & page links automatically | Ready for `/v1/batch` traversal |

## Normalized Multi-Row Table JSON Schema
```json
{
  "type": "object",
  "required": [
    "tables"
  ],
  "properties": {
    "tables": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": [
          "headers",
          "rows"
        ],
        "properties": {
          "headers": {
            "type": "array",
            "minItems": 2
          },
          "rows": {
            "type": "array",
            "minItems": 1
          }
        }
      }
    }
  }
}
```

## Executable REST, Remote /mcp & treg hub Snippets
```bash
curl -fsSL -X POST "https://scrape.fastmesh.dev/v1/extract" \
  -H "Content-Type: application/json" \
  -d '{
    "content_text": "<table><thead><tr><th rowspan=\"2\">Plan</th><th colspan=\"2\">Pricing</th></tr><tr><th>USD</th><th>Credits</th></tr></thead><tbody><tr><td>Starter</td><td>$15</td><td>1,000</td></tr><tr><td>Growth</td><td>$59</td><td>5,000</td></tr></tbody></table>",
    "format_hint": "html_table"
  }'

curl -fsSL -X POST "https://scrape.fastmesh.dev/mcp" \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"scrape_table","arguments":{"url":"https://example.org/rates"}}}'
```

## Frequently Asked Questions

### How does Scrape-Anything flatten two-row `<thead>` headers?
When a table has multiple header rows with `colspan` and `rowspan`, the 2D grid tracks parent group labels above child columns and combines distinct header segments into a clean, deduplicated key per column.

### Can I submit raw HTML directly without a public URL?
Yes. Send `content_text` with `content_type: "text/html"` and `format_hint: "html_table"` to `/v1/extract` or the `scrape_table` MCP tool.

### What if a page contains zero tables when I require one?
Attach `minItems: 1` on `tables` in `json_schema`. If no table exists, validation returns `schema_valid: false` and `billed_micros: 0`.

## Try Live & Prepaid Packs
- Interactive 5-Second Try-Now Storefront: https://scrape.fastmesh.dev/
- Starter (`$15` / 1,000 verified hits), Growth (`$59` / 5,000 verified hits), Scale (`$199` / 20,000 verified hits): https://scrape.fastmesh.dev/#pricing
- Remote MCP: `https://scrape.fastmesh.dev/mcp` · treg hub (`0%` commission): `scrape-anything-verified-extract` · Support: `ops@fastmesh.dev`
