Registry / serialization / json2html

json2html

JSON →
library1.3.0pypypi✓ verified 23d ago

json2html is a Python wrapper designed to convert JSON objects into human-readable HTML table representations. It provides functionalities to transform nested JSON structures into styled HTML tables. The current version is 1.3.0, and while updates are infrequent, it remains an active and functional library for its intended purpose.

pip install json2html
INSTALL
IMPORT
SIG · JSON2HTML
J
json2html
serializationpythonv1.3.0
Install
2.3s avg
Import
31ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.032s · 19.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.3s · import 0.030s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

json2html
from json2html import json2html
While 'from json2html import *' is often seen, importing the 'json2html' class explicitly is clearer.

This quickstart demonstrates how to convert a Python dictionary (representing JSON data) into an HTML table string using the `json2html.convert` method. It also shows how to apply custom HTML attributes to the generated table.

from json2html import json2html json_data = { "name": "Alice", "details": [ {"age": 30, "city": "New York"}, {"age": 25, "city": "London"} ] } html_output = json2html.convert(json=json_data, table_attributes="id=\"info-table\" class=\"table table-bordered\"") print(html_output)
Debug
Known issues
gotchaThe 'json' parameter of the `convert` method expects a Python dictionary or list, not a JSON string. If you have a JSON string, you must parse it first using `json.loads()`.
fix
Ensure your input is a Python object: `import json; data_obj = json.loads(json_string); html = json2html.convert(json=data_obj)`.
affects: All versions
gotchaBy default, HTML tags within text nodes are escaped to prevent Cross-Site Scripting (XSS) attacks. If you intend to render raw HTML content within your JSON values, you must explicitly disable escaping.
fix
Pass `escape=False` to the `convert` method: `json2html.convert(json=my_data, escape=False)`.
affects: All versions
gotchaWhen a JSON value is an array of objects with identical keys, `json2html` will 'club' them together into a single row by default for a more compact representation. If you need each object in the array to generate a separate row, you must disable this feature.
fix
Pass `clubbing=False` to the `convert` method: `json2html.convert(json=my_data, clubbing=False)`.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'json2html'
The 'json2html' library has not been installed in the current Python environment.
fix
pip install json2html
AttributeError: 'str' object has no attribute 'keys'
The `json2html.convert` method received a JSON string as input instead of a parsed Python dictionary or list. The library expects a Python object (dict or list) for its 'json' argument, not a raw JSON string.
fix
Ensure the JSON data is parsed into a Python dictionary or list using `json.loads()` before passing it to `json2html.convert`.

```python
import json
from json2html import * 

json_string = '{"name": "json2html", "description": "Converts JSON to HTML tabular representation"}'
python_object = json.loads(json_string)
html_table = json2html.convert(json = python_object)
print(html_table)
```
TypeError: Can't convert NULL!
This error occurs when the `json2html` library encounters a `None` (Python null) value in the JSON data that it cannot process or convert to an HTML representation directly.
fix
Modify the input JSON to handle or remove `None` values, perhaps by replacing them with empty strings or other default values, or by preprocessing the data before passing it to `json2html.convert`.

```python
from json2html import * 

# Original input with a None value that might cause the error
# input_data_with_null = {"item": "value", "empty_field": None}

# Fix: Replace None with an empty string or remove the key if desired
input_data_fixed = {"item": "value", "empty_field": ""}
# Or remove the key: input_data_fixed = {"item": "value"}

html_table = json2html.convert(json = input_data_fixed)
print(html_table)
```
TypeError: list indices must be integers, not str
This typically happens when processing nested JSON structures where a list is expected, but an attempt is made to access it using string keys (like a dictionary), or vice-versa, within the `json2html`'s internal processing. This often indicates a mismatch between the input JSON structure and what the library's conversion logic expects for certain operations.
fix
Review the structure of your JSON input to ensure consistency between lists and dictionaries in nested levels. If `json2html` expects a list of dictionaries for a specific feature (like clubbing), ensure your data adheres to that structure. Ensure all list elements are indeed accessed via integer indices and dictionary elements via string keys.

```python
from json2html import * 

# Example of expected structure for clubbing (list of dictionaries)
correct_input = {
    "products": [
        {"id": 1, "name": "Laptop"},
        {"id": 2, "name": "Mouse"}
    ]
}
html_table = json2html.convert(json = correct_input)
print(html_table)

# If you had a list directly, ensure json2html can handle it at the top-level
# or wrap it in a dictionary if the specific conversion requires a top-level dict.
list_input = [
    {"id": 1, "name": "Laptop"},
    {"id": 2, "name": "Mouse"}
]
html_table_list = json2html.convert(json = list_input) # This should generally work now per library updates
print(html_table_list)
```
Upgrade
Version history
1.3.0latest on PyPI · released Jul 3, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
json2html — pip install json2html · libregistry