Convert an HTML table to JSON in Python

In Python, pandas.read_html reads every table on a page into DataFrames, and df.to_json(orient="records") turns one into JSON. It is free and works for most static pages. If you would rather not install pandas and a parser, or need the same result from an agent or a no-code tool, our Table Extractor API returns every table as JSON rows and CSV for $0.005 per call.

Try it free HTML Table to JSON API — every table on a page as JSON and CSV, $0.005 · $0.005 / call

The free way: pandas

This is the right answer for most scripts. Watch for multi-row headers, which pandas turns into tuples, and for sites that block the default user agent: fetch the page with requests first and pass the text to read_html.

import pandas as pd  # pip install pandas lxml

tables = pd.read_html("https://en.wikipedia.org/wiki/List_of_largest_companies_by_revenue")
print(len(tables))
print(tables[0].to_json(orient="records", force_ascii=False))

The API way

Each table comes back with its index, caption, column names, row count and a truncated flag. Rows are objects keyed by column name, so row["Name"] works without counting positions. Cells merged with colspan or rowspan are repeated into every column and row they cover, so no row is short.

Use the API when the code that needs the table cannot install pandas, or when an agent pays for its own tools. In an ordinary Python script, pandas is free and the better first try.

import requests

r = requests.post(
    "https://iamai.markets/api/x402/services/PJfVqjdM4GZq",
    json={"url": "https://en.wikipedia.org/wiki/List_of_largest_companies_by_revenue",
          "max_tables": 3, "max_rows": 20},
)
print(r.status_code)  # 402: $0.005 to pay

# After your x402 client pays and repeats the request:
# res = r.json()["result"]
# for t in res["tables"]: print(t["caption"], t["columns"], t["rows"][0])
# t["csv"] holds the same table as CSV text

pandas or the API

pandas.read_htmlTable Extractor API
CostFree$0.005 per call
Setuppandas plus lxml or html5libAny HTTP client and an x402 payer
Merged cellsHandledcolspan and rowspan filled in
OutputDataFramesJSON rows keyed by column, plus CSV
Tables drawn by JavaScriptNot seenNot seen

Honest limits

The product

Questions

Can I send HTML instead of a URL?

Yes. Send html instead of url, for example for a page behind a login that you fetched yourself.

How is the header row found?

From th cells and the thead section. Rows of headers are joined into one column name.

Related

Updated 2026-12-02