Skip to content
MNB PHPExcelDeveloper Guide
AI

JSON library

Writing JSON Output

Write row arrays, serialize complete workbook objects, and return JSON strings for APIs without saving files.

Level
Intermediate
Reading time
7 min
Package
mnb/mnb-phpexcel-json
Updated
Json::write()JsonWriter::writeWorkbook()JsonWriter::payloadToString()

Write rows with the format facade#

PHP
Json::write($rows, 'products.json', [
    'pretty' => true,
    'preserve_zero_fraction' => true,
    'trailing_newline' => true,
]);

Serialize a complete workbook#

PHP
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#

PHP
$payload = Json::read('orders.json')
    ->withHeaderRow(1)
    ->toStructuredWorkbookArray();

$json = (new JsonWriter())->payloadToString($payload, [
    'pretty' => false,
]);