Writing JSON Output
Write row arrays, serialize complete workbook objects, and return JSON strings for APIs without saving files.
Updated
Json::write()JsonWriter::writeWorkbook()JsonWriter::payloadToString()What you will learn
- Write normalized rows with stable scalar conversion.
- Choose rows, single-sheet wrapper, or multi-sheet workbook shapes.
- Return prebuilt structured payloads as JSON strings.
Write rows with the format facade#
Json::write($rows, 'products.json', [
'pretty' => true,
'preserve_zero_fraction' => true,
'trailing_newline' => true,
]);Serialize a complete workbook#
use Mnb\PHPExcel\Writer\JsonWriter;
$writer = new JsonWriter();
$json = $writer->workbookToString($workbook, [
'mode' => 'auto',
'include_metadata' => true,
'include_sheet_names' => true,
'preserve_associative_rows' => true,
]);Return structured output directly#
$payload = MnbExcel::read('orders.xlsx')
->withHeaderRow(1)
->toStructuredWorkbookArray();
$json = (new JsonWriter())->payloadToString($payload, [
'pretty' => false,
]);Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue