json2table
raw JSON → 1.1.5 verified Mon Apr 27 auth: no python
A library to convert JSON or Python dicts into an HTML table. Current version 1.1.5. Low release cadence.
pip install json2table Common errors
error TypeError: 'module' object is not callable ↓
cause Importing the whole module and trying to call it as a function, e.g. `json2table(data)`.
fix
Use
from json2table import convert then call convert(data). error AttributeError: module 'json2table' has no attribute 'convert' ↓
cause Conflicting namespace or incorrect import path. Possibly having another module named json2table in the path.
fix
Check for local files named json2table.py and rename them. Use pip list to verify installed version.
error TypeError: string indices must be integers ↓
cause Passing a JSON string instead of a Python dict. The library expects a parsed dict.
fix
Parse JSON string first:
import json; data = json.loads(json_string); convert(data). Warnings
breaking The order of dictionary keys is not preserved. Python dicts preserve insertion order from 3.7+, but json2table may sort keys alphabetically. ↓
fix Use an OrderedDict if key order matters, or check the generated HTML order.
gotcha Nested JSON objects are expanded into sub-tables by default. This can produce deeply nested HTML if not controlled. ↓
fix Use the `max_level` parameter to limit depth or customize with `build_converter`.
gotcha The `table_attributes` parameter expects a dict, not a string. Common mistake is passing HTML attributes as a string like 'class="my-table"'. ↓
fix Pass a dict: {'class': 'my-table'}.
Imports
- convert wrong
import json2tablecorrectfrom json2table import convert
Quickstart
from json2table import convert
json_data = {
"name": "Alice",
"age": 30,
"address": {
"street": "123 Maple St",
"city": "Springfield"
}
}
table_attributes = {"style": "width:100%"}
table = convert(json_data, table_attributes=table_attributes)
print(table)