JsonHandler — Aspose.Cells FOSS for Python API Reference
aspose-cells-foss can export worksheet data to JSON via the JsonHandler class. The output represents each row as a JSON object keyed by column header, collected into an array.
Package: aspose.cells_foss
from aspose.cells_foss import JsonHandler, JsonSaveOptions
from aspose.cells_foss import save_workbook_as_jsonJsonSaveOptions
Options that control how the worksheet is serialized to JSON.
Constructor
JsonSaveOptions()Properties
JsonSaveOptions has no configurable properties. Pass an instance to JsonHandler.save_json() to use default serialization behavior.
JsonHandler
JsonHandler contains only static methods and is never instantiated.
JsonHandler.save_json
JsonHandler.save_json(workbook, file_path: str, options: JsonSaveOptions | None = None) -> NoneExports the first worksheet to a JSON file at file_path. If options is None, default JsonSaveOptions are used.
JsonHandler.save_json_to_dict
JsonHandler.save_json_to_dict(workbook, options: JsonSaveOptions | None = None) -> dictSerializes the worksheet to a Python dict (or list) and returns it without writing any file. Useful for programmatic post-processing.
Module Function
save_workbook_as_json
save_workbook_as_json(workbook, file_path: str, options: JsonSaveOptions | None = None) -> NoneConvenience wrapper around JsonHandler.save_json(workbook, file_path, options).
Example
The following example writes a sales table and exports it to a JSON file with ISO date formatting.
from aspose.cells_foss import Workbook, JsonHandler, JsonSaveOptions
import datetime
wb = Workbook()
ws = wb.worksheets[0]
ws.name = "Sales"
# Header row
ws.cells["A1"].put_value("Date")
ws.cells["B1"].put_value("Product")
ws.cells["C1"].put_value("Amount")
# Data rows
ws.cells["A2"].put_value(datetime.date(2025, 1, 15))
ws.cells["B2"].put_value("Widget A")
ws.cells["C2"].put_value(1250.00)
ws.cells["A3"].put_value(datetime.date(2025, 1, 22))
ws.cells["B3"].put_value("Widget B")
ws.cells["C3"].put_value(840.50)
opts = JsonSaveOptions()
# Save to file
JsonHandler.save_json(wb, "sales.json", options=opts)
# Or retrieve as a Python dict
data = JsonHandler.save_json_to_dict(wb, options=opts)
print(data)Example output (sales.json):
{
"Sales": [
{"Date": "2025-01-15", "Product": "Widget A", "Amount": 1250.0},
{"Date": "2025-01-22", "Product": "Widget B", "Amount": 840.5}
]
}