PHP code example of michaelalexeevweb / php-json-chunk
1. Go to this page and download the library: Download michaelalexeevweb/php-json-chunk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
michaelalexeevweb / php-json-chunk example snippets
$reader = new PhpJsonChunk\JsonChunkReader();
foreach ($reader->readGenerator(__DIR__ . '/data.json', keyPath: 'data.0.items') as $item) {
echo $item['id'], PHP_EOL;
}
declare(strict_types=1);
use PhpJsonChunk\JsonChunkReader;
$reader = new JsonChunkReader();
$stream = $reader->readGenerator(
filePath: __DIR__ . '/large-data.json',
chunkSize: 1000,
);
foreach ($stream as $chunk) {
// Does not load the full JSON file into memory.
foreach ($chunk as $item) {
echo $item['id'] . PHP_EOL;
}
}
declare(strict_types=1);
use PhpJsonChunk\JsonChunkReader;
$reader = new JsonChunkReader();
$items = $reader->readGenerator(
filePath: __DIR__ . '/payload.json',
chunkSize: 500,
keyPath: 'data.0.items',
);
foreach ($items as $chunk) {
var_dump($chunk);
}
$items = $reader->readGenerator(
filePath: __DIR__ . '/payload.json',
chunkSize: 500,
keyPath: 'key1.*.key2.*.key3',
);
use PhpJsonChunk\Source\StringSource;
use PhpJsonChunk\Source\StreamSource;
$payload = '{"items":[{"id":1},{"id":2}]}';
$reader->read(__DIR__ . '/data.json', keyPath: 'items');
$reader->read(new StringSource($payload, 'the upload'), keyPath: 'items');
$handle = fopen('php://memory', 'r+b');
fwrite($handle, $payload);
rewind($handle);
$reader->read(new StreamSource($handle), keyPath: 'items');
foreach ($reader->readGenerator(__DIR__ . '/users.json') as $id => $user) {
echo $id, ': ', $user['name'], PHP_EOL;
}
$reader->read(__DIR__ . '/data.json', keyPath: 'data.0.items'); // the short form
$reader->read(__DIR__ . '/data.json', keyPath: ['a.b']); // a key containing a dot
foreach ($reader->readPaths(__DIR__ . '/data.json', ['users', 'logs']) as $path => $value) {
echo $path, ': ', json_encode($value), PHP_EOL;
}
$reader = new JsonChunkReader(associative: false); // items come back as stdClass
$reader->forEach(__DIR__ . '/data.json', function (array $item): bool {
return $item['id'] < 1000; // stop once the ids get big
});
declare(strict_types=1);
use PhpJsonChunk\JsonChunkReader;
$reader = new JsonChunkReader();
$total = $reader->count(
filePath: __DIR__ . '/data.json',
keyPath: 'data.0.items',
);
declare(strict_types=1);
use PhpJsonChunk\JsonChunkReader;
$reader = new JsonChunkReader();
$chunks = $reader->read(
filePath: __DIR__ . '/data.json',
chunkSize: 2,
limit: 10,
offset: 0,
keyPath: null,
tempChunkDir: null,
);
declare(strict_types=1);
use PhpJsonChunk\JsonChunkReader;
$reader = new JsonChunkReader();
$iterator = $reader->readIterator(
filePath: __DIR__ . '/data.json',
chunkSize: null,
limit: 100,
offset: 200,
);
foreach ($iterator as $item) {
var_dump($item);
}
declare(strict_types=1);
use PhpJsonChunk\JsonChunkReader;
$reader = new JsonChunkReader();
$generator = $reader->readGenerator(
filePath: __DIR__ . '/data.json',
chunkSize: 2,
limit: null,
offset: 0,
keyPath: 'key1.0.key2.0.key3',
tempChunkDir: null,
);
foreach ($generator as $chunk) {
var_dump($chunk);
}
declare(strict_types=1);
use PhpJsonChunk\JsonChunkReader;
$reader = new JsonChunkReader();
$first = $reader->getFirst(__DIR__ . '/data.json', keyPath: 'data');
var_dump($first);
declare(strict_types=1);
use PhpJsonChunk\JsonChunkReader;
$reader = new JsonChunkReader();
$last = $reader->getLast(__DIR__ . '/data.json', keyPath: 'data');
var_dump($last);
declare(strict_types=1);
use PhpJsonChunk\JsonChunkReader;
$reader = new JsonChunkReader();
$tenth = $reader->getNth(__DIR__ . '/data.json', index: 10, keyPath: 'data');
var_dump($tenth);
declare(strict_types=1);
use PhpJsonChunk\JsonChunkReader;
$reader = new JsonChunkReader();
$total = $reader->forEach(
__DIR__ . '/data.json',
callback: function ($item) {
echo $item['name'] . "\n";
},
keyPath: 'data',
);
echo "Processed $total records\n";
declare(strict_types=1);
use PhpJsonChunk\JsonChunkReader;
$reader = new JsonChunkReader();
// Two documents, because they are shaped differently: a keyPath of null needs a file whose ROOT is
// an array, and a nested keyPath needs one whose root is an object. One file cannot be both.
$filePath = __DIR__ . '/data.json'; // [{"id": 1}, {"id": 2}, ...]
$nestedFilePath = __DIR__ . '/nested.json'; // {"key1": [{"key2": [{"key3": [...]}]}]}
$namesFilePath = __DIR__ . '/names.json'; // {"data": [{"name": "Alice"}, {"name": "Bob"}]}
// Returns total number of items in target array
$total = $reader->count(
filePath: $filePath,
);
// Returns one chunk with all items from target list
$all = $reader->read(
filePath: $filePath,
chunkSize: null,
limit: null,
offset: 0,
keyPath: null,
tempChunkDir: null,
);
// Returns chunks of 2 items
$chunks = $reader->read(
filePath: $filePath,
chunkSize: 2,
limit: null,
offset: 0,
keyPath: null,
tempChunkDir: null,
);
// Read from nested key path (example: key1.0.key2.0.key3)
$nested = $reader->read(
filePath: $nestedFilePath,
chunkSize: null,
limit: null,
offset: 0,
keyPath: 'key1.0.key2.0.key3',
tempChunkDir: null,
);
// Limit and offset support
$window = $reader->read(
filePath: $filePath,
chunkSize: null,
limit: 10,
offset: 20,
keyPath: null,
tempChunkDir: null,
);
// Optional directory for temporary chunk files used by read()
$windowWithTempChunks = $reader->read(
filePath: $filePath,
chunkSize: 500,
limit: 10_000,
offset: 0,
keyPath: null,
tempChunkDir: __DIR__ . '/var/chunks',
);
// Total stays independent from limit/offset
$totalNested = $reader->count(
filePath: $nestedFilePath,
keyPath: 'key1.0.key2.0.key3',
);
// Iterator with plain items (memory-friendly for large files)
$iterator = $reader->readIterator(
filePath: $filePath,
chunkSize: null,
limit: 2,
offset: 1,
keyPath: null,
tempChunkDir: null,
);
foreach ($iterator as $item) {
var_dump($item);
}
// Optional directory for temporary chunk files used by readIterator()
$iteratorWithTempChunks = $reader->readIterator(
filePath: $filePath,
chunkSize: 500,
limit: 10_000,
offset: 0,
keyPath: null,
tempChunkDir: __DIR__ . '/var/chunks',
);
// Generator with chunks
$generator = $reader->readGenerator(
filePath: $filePath,
chunkSize: 2,
limit: null,
offset: 0,
keyPath: null,
tempChunkDir: null,
);
foreach ($generator as $chunk) {
var_dump($chunk);
}
// Optional directory for temporary chunk files used by readGenerator()
$generatorWithTempChunks = $reader->readGenerator(
filePath: $filePath,
chunkSize: 500,
limit: 10_000,
offset: 0,
keyPath: null,
tempChunkDir: __DIR__ . '/var/chunks',
);
// Iterator from nested key path with limit/offset
$iteratorNested = $reader->readIterator(
filePath: $nestedFilePath,
chunkSize: null,
limit: 10,
offset: 0,
keyPath: 'key1.0.key2.0.key3',
tempChunkDir: null,
);
foreach ($iteratorNested as $item) {
var_dump($item);
}
// Wildcard traversal — iterate all items at a given array level using "*"
// JSON: {"key1":[{"key2":[{"key3":[1,2]},{"key3":[3,4]}]},{"key2":[{"key3":[5]}]}]}
// keyPath "key1.*.key2.*.key3" will collect all key3 arrays and stream their items
$wildcardGenerator = $reader->readGenerator(
filePath: $nestedFilePath,
keyPath: 'key1.*.key2.*.key3',
);
foreach ($wildcardGenerator as $item) {
var_dump($item); // yields items from every matched key3 array
}
// Wildcard on scalar field — stream a flat value from every array element
// JSON: {"data":[{"name":"Alice"},{"name":"Bob"}]}
// keyPath "data.*.name" yields "Alice", "Bob"
$names = $reader->readGenerator(
filePath: $namesFilePath,
limit: 10,
keyPath: 'data.*.name',
);
foreach ($names as $name) {
echo $name . PHP_EOL;
}
bash
php bin/benchmark.php --runs=2 --sizes=10000,50000,100000,500000,1000000,5000000
bash
php bin/jsontestsuite.php # fetches the corpus into a temporary directory
php bin/jsontestsuite.php --corpus=/path/to/JSONTestSuite/test_parsing