MNB PHPExcelDeveloper Guide
v2.0.5
JSON module

Reading JSON and NDJSON

Stream top-level arrays and line-delimited records, select workbook sheets, and control normalized columns.

Updated

LevelIntermediateReading time8 minPackagemnb/mnb-phpexcel
Json::read()sheetNames()streaming()projectColumns()

What you will learn

  • Stream standard arrays or NDJSON without loading the entire source.
  • Define stable columns for heterogeneous records.
  • Select named sheets from workbook-shaped JSON.

Stream a top-level array#

PHP
$rows = Json::read('events.json', [
    'stream_json_array' => true,
    'json_columns' => ['id', 'type', 'created_at'],
    'streaming_extra_keys' => 'error',
    'max_json_record_bytes' => 2 * 1024 * 1024,
])->withHeaderRow(1)->streaming()->rows();

Read NDJSON explicitly#

PHP
$rows = Json::read('events.ndjson', [
    'json_document_mode' => 'ndjson',
    'ignore_blank_lines' => true,
    'max_source_rows' => 1000000,
])->withHeaderRow(1)->rows();

Select a sheet from workbook-shaped JSON#

PHP
$session = Json::read('workbook.json', [
    'json_document_mode' => 'json',
]);

print_r($session->sheetNames());
$orders = $session->sheet('Orders')->withHeaderRow(1)->rows();