Skip to content
MNB PHPExcelDeveloper Guide
AI

JSON library

NDJSON and Structured JSON Workflows

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

Level
Advanced
Reading time
8 min
Package
mnb/mnb-phpexcel-json
Updated
toStructuredJson()saveStructuredJson()json_columnsstreaming_extra_keys

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();

Return a structured JSON response#

PHP
$json = Json::read('report.json')
    ->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.