MNB PHPExcelDeveloper Guide
JSON module

NDJSON and Structured JSON Workflows

Design stable streaming schemas, handle extra keys, and bridge spreadsheet sessions to structured JSON responses.

Updated

LevelAdvancedReading time8 minPackagemnb/mnb-phpexcel
toStructuredJson()saveStructuredJson()json_columnsstreaming_extra_keys

What you will learn

  • Define a deterministic column order for streamed records.
  • Choose whether unknown record keys are ignored or rejected.
  • Convert any compatible read session into workbook-aware JSON.

Pin a streaming schema#

PHP
$rows = Json::read('audit.ndjson', [
    'json_document_mode' => 'ndjson',
    'json_columns' => ['event_id', 'actor', 'action', 'occurred_at'],
    'include_header_row' => true,
    'streaming_extra_keys' => 'error',
])->withHeaderRow(1)->rows();

Convert XLSX to a structured JSON response#

PHP
$json = MnbExcel::read('report.xlsx')
    ->withHeaderRow(1)
    ->toStructuredJson(
        readOptions: ['preserve_original_row_numbers' => true],
        jsonOptions: ['pretty' => true]
    );

header('Content-Type: application/json; charset=UTF-8');
echo $json;

Apply defensive limits#

  • Set max_file_bytes before parsing uploads.
  • Set max_json_record_bytes for NDJSON and streamed arrays.
  • Use max_source_rows and row slicing for bounded work.
  • Use depth to cap JSON nesting accepted by the decoder.